Domanda

Below is the code that I have to setup the ObjectStore and it works fine.

- (RKManagedObjectStore *)setupCoreDataWithRESTKit{
    NSError * error;
    NSURL * modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"app" ofType:@"momd"]];
    NSManagedObjectModel * managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
    self.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

    [self.managedObjectStore createPersistentStoreCoordinator];

    NSArray * searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentPath = [searchPaths objectAtIndex:0];
    NSPersistentStore * persistentStore = [self.managedObjectStore addSQLitePersistentStoreAtPath:[NSString stringWithFormat:@"%@/app.sqlite", documentPath] fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
    NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

    if(!persistentStore){
        NSLog(@"Failed to add persistent store: %@", error);
    }

    [self.managedObjectStore createManagedObjectContexts];
    return self.managedObjectStore;
}

- (id)optionsForSqliteStore {
    return @{
             NSInferMappingModelAutomaticallyOption: @YES,
             NSMigratePersistentStoresAutomaticallyOption: @YES
             };
}

Question 1: How often do I need to call this code? Just once when the Application is launched the first time? Or each time the application is launched? Or only one time per launch when I need access to the store?

Question 2: I cache the data per User. If the User logs out of the app, I would like to delete all of the data/objects (or the datastore?), any suggestion on what's the best practice to do this?

È stato utile?

Soluzione

How often do I need to call this code?

each time the application is launched

I cache the data per User. If the User logs out of the app, I would like to delete all of the data/objects (or the datastore?), any suggestion on what's the best practice to do this?

If you want to delete everything, then destroy the managed object context and store coordinator and delete the SQLite file from disk.

If you just want to delete some things then delete the managed objects from the context and save the context (and persistent store).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top