2
votes

Question: What will be the best way to handle bunch of data that just some of the objects should be saved to disk in coredata?

This is the app/problem: The app will provide users with the possibility to search for different items on the internet. The search will return a number of objects that will be displayed to the user. The user should be able to favorites any of these object at any time. Objects that has been favored should be connected to the current logged in user and live after the app has quit.

The app will have iOS6 as base.

I have been using using these resources

Im currently looking into the parent/child approach with use of 3 Contexts: Master, Main and Confinement context types.

Current possible solution:

  • MasterContext that perform save on disk (Has the persistentStoreCoordinator)
  • MainContext that is used by the UI (child of the masterContext)
  • BackgroundContext to handle new objects from searches. (child of the mainContext)

So the user may do a search that will return 100 objects (imported on the background context and saved up to the main context). 2 of these objects are favored by the user (on the maincontext). The object will be added to the user and set as "should be saved". (On a save the objects will be pushed up to the master context)

When I save the mastercontext I dont want to save all the 100 objects to disk. Just the two objects the user have favored.

So im think about deleting the object that should not be saved to disk just before I do a save on the mastercontext.

- (void) mainContextHasSaved: (NSNotification *) notification {
NSLog(@"Lets save the master");

[_masterManagedObjectContext performBlock:^{
    //Loop through all inserted object and check if they should be saved to disk
    [self removeObjectThatShouldNotBeSavedToDisk];

    NSError *error = nil;
    BOOL saveSuccess = [_masterManagedObjectContext save:&error];

    if(saveSuccess) {
        //Do something
    }
}];
}

But after what I understood is that when a save is performed on a parent context, all the changes will be propagated to the children. Then I will loose all the objects except the two that has been stored.

So does anyone know how to solve this kind of problem? Is it something I can do in the example presented above? Or should I create multiple persistentStores and move objects between contexts?

Thanks to all that is willing to help and if more information is needed just ask :)

1
Create objects for all results, then delete the non-favorited ones. Only save when the app is put in the background. - nielsbot
Also, why not use a query favorited == NO to get the items to delete? - nielsbot
But if a user is looking on a search result, favor some objects and closes the app. I will think the user would like to still have the data when the app is opened from the background. - Thomas Torp
Yes--but no need to call save too often I mean. Unless you're afraid of crashes :) - nielsbot

1 Answers

0
votes

In a similar project I used this solution which was also favored by the users:

Keep a time stamp attribute in the downloaded items and delete them when the time stamp is older than a certain threshold and they are not marked as favorite.