1

For some reasons, I have to update my core data model at every app update.

The problem is, at first launch of every update (only at first launch of every update), I would like to reset my model but I don't know how to proceed.

Is someone have ever had this kinf of problem ?

Thanks a lot in advance !

Vinestro
  • 1,072
  • 1
  • 10
  • 32

2 Answers2

2

Have you enabled Persistent Store migration? As long as you are only making trivial model modifications, automatic migration will mean you don't need to reset the data.

 NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                               configuration:nil
                                                         URL:storeURL
                                                     options:@{
                                                        NSMigratePersistentStoresAutomaticallyOption:@YES,
                                                        NSInferMappingModelAutomaticallyOption:@YES,
                                                        NSSQLiteAnalyzeOption:@YES,
                                                            }
                                                       error:&error])

If there's a different reason to clear your model, then NSUserDefaults is the way to go. You can use a #define to set a version number, and have the persistent store reset when the numbers are different, then when you know you've made a change that requires an empty store you can increment the #define:

#define kNukeTheCache 1000

...

    NSNumber *nukeNumber = [[NSUserDefaults standardUserDefaults] objectForKey:nukemeKey];

    if ((nukeNumber == nil) || (nukeNumber.integerValue < kNukeTheCache)) {
            [self deletePersistentStore:storeURL];
    }
    [[NSUserDefaults standardUserDefaults] setInteger:kNukeTheCache forKey:nukemeKey];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:totalNuke];
    [[NSUserDefaults standardUserDefaults] synchronize];


- (void)deletePersistentStore:(NSURL *)storeURL
{
    NSError *error = nil;
    [[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error];
    if (error) { // Handle error }
}
ikuramedia
  • 6,038
  • 3
  • 28
  • 31
0

Use NSUserDefaults to set the application whether updated or not .If updated then clear the core data from application.Follow this link to reset the core data:link

when your update is done:

[[NSUserDefaults standardUserDefaults]setBool:TRUE forKey:@"isUpdated"]

then check the value of the same when you are launching the application.

Community
  • 1
  • 1
V-Xtreme
  • 7,230
  • 9
  • 39
  • 79
  • Ok, so I have to set a variable (boolean) with a certain value (true), then change it to (false) after the first run ? – Vinestro Jan 15 '13 at 10:33
  • Ok I see. The problem is I need to reset Core data only one time at every app update, or if I set a new variable in NSUserDefaults, this variable will already be set at the next app update (NSUserDefaults are not reset at app update). – Vinestro Jan 15 '13 at 11:13
  • what do you mean by app update ? is it updating the application data ? – V-Xtreme Jan 15 '13 at 11:53
  • Non, I mean, deploy a new version of the app on devices. – Vinestro Jan 15 '13 at 12:43