Saving Single CoreData Entity (Not the Whole Context) While Keeping NSFetchedResultController Functionality

邮差的信 提交于 2019-12-03 12:38:55

I think the easiest thing to do would be to have a second NSPersistentStore attached to your persistent store coordinator. You can have that store be an in-memory store, and store all your "online" results in that (temporary) store. You can specify which store a newly-inserted object should be saved in with assignObject:toPersistentStore:. Once you've done this, you can save freely, since the "save" will only happen to memory for your online songs.

Then, when you want to move a song from the online set to the permanent set, just delete it and re-insert it, assigning the new object to your permanent persistent store with the same method.

This will let you use a single NSManagedObjectContext attached to your NSPersistentStoreCoordinator, which will see objects from both of the NSPersistentStores.

Jesse's solution will work just fine.

However, as another option, you can simply use a nested context, just like you would for a detailed inspector.

That context can hold all your "temporary" items, but since it is a child of your "saving" context, all fetches will work just fine.

NSManagedContext *tempContext = [[NSManagedContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
tempContext.parentContext = mainManagedObjectContext;

ALl your saves will be inserted into mainManagedObjectContext, and saved with save:. All your temporary items will go into tempContext.

Attach your fetched results controller to the tempContext as well.

When you are ready to get rid of the temporary items, just set the tempContext to nil.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!