When I create a new NSManagedObject subclass I loose all my data. Way to avoid this?

Discussion in 'iOS Programming' started by whitedragon101, Jun 18, 2014.

  1. whitedragon101 macrumors 6502a

    Joined:
    Sep 11, 2008
    #1
    When using core data I sometimes think oh I need a new entity or attribute. When I add these I click the "create new NSManagedObjectSubclass" button to get access to these new entities or fields. However, when I fire up the simulator it doesn't work as it says it was created using a different data model. So I delete the app start again but all the data is gone.

    Is there a way to avoid this?

    This is going to be crucial for updating the app for future versions because it won't be good for the user to loose all their recorded information every time I need to add an entity or attribute.
     
  2. Ubuntu macrumors 68000

    Ubuntu

    Joined:
    Jul 3, 2005
    Location:
    UK/US
    #2
    I think you'll need to make a new version of the model and then develop a migration policy (so that you can handle the changes between model versions, such as the entities/attributes you've added/removed/modified). As far as I know that's the preferred way of dealing with changes in the model.

    More information from Apple.
     
  3. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #3
    ^ as above bonus being if your only adding new entities or adding new attributes to you existing entities then you can take advantage of Auto migration. Auto migration will take care of a lot of model change these days.

    Still as above at a minimum you need to make model versions for it to work.
     
  4. whitedragon101 thread starter macrumors 6502a

    Joined:
    Sep 11, 2008
    #4
    Thanks guys.

    I really hope they make that easier in the future maybe with all the new swift and xcode 6 stuff. I didn't realise this would be an issue. If you add a table or field to a sql db it doesn't kill your app.
     

Share This Page