[iOS] Switching storage files

April 20, 2016

As we all know, Core data is a amazing framework of Cocoa let us easily working with data in iOS app. Today, I’m gonna write down something about an interested topic.

So topic is:
In case we want to switch to use physical storage location, you could imagine user want to use a fews of accounts, so we must save each storage file for each account. This will cause the app will load faster because it will show on screen the loaded data, and there will be some background fetches if needed.

Switching storage files

There is an sample that I wrote, and easy to understand. Beside mainly working with switching storage files, the way how we work with NSFetchedResultsController and UITableView included. This is a cool feature that Apple supplied. I give you a thumb up, Apple.

Sample project here

Back to the thread, switching between storage files is just a simple. We basically remove all the persistenstores from persistenstoreCoordinator and re-add it from over. But first, don’t forget to save the state.

Here is it:

-(void)reloadStore {

    // save state
    
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSError *error = nil;
    if (![context save:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    
    // remove all persistent stores
    
    for (NSPersistentStore *store in _persistentStoreCoordinator.persistentStores) {
        [_persistentStoreCoordinator removePersistentStore:store error:nil];
    }
    
    // add new persistent store with specified storage location
    
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:[self storageName];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                   configuration:nil
                                                             URL:storeURL
                                                         options:[self storeOptions]
                                                           error:nil]) {
        NSLog(@"Can not add Persistent store.");
        return;
    }

}

More over, I would like to suggest a valuable link to get fully understand about core data; This serials are quite easy to understand, and hope we could gain from it.

Migration

Beside that, Apple supplies a cool feature named Migration, that we can migrate between storage locations, and types of data. More details, it is nice to read this one;

But notice that, this one is not switching, this is in fact merge from old one with new one. So that carefully when working between them two.

Finally, if my project is good for you, don’t hesitate to give it a star. haha.