iOS RestKit can not save local entity to database

本秂侑毒 提交于 2019-12-03 05:51:51

Thanks for the lead by Tom, I found that RestKit has NSManagedObjectContext (RKAdditions), which has a method:

- (BOOL)saveToPersistentStore:(NSError **)error

Yes it does have logic to handle nested managed object context. Here is the new code that works, just one line change, but took a lot of time to find the right call :(

#import "NSManagedObjectContext+RKAdditions.h"
     NSManagedObjectContext *managedObjCtx = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
     MyClass* course = [managedObjCtx insertNewObjectForEntityForName:@"MyClass"];

     .. set the data for course here

     NSError *executeError = nil;
     if(![managedObjCtx saveToPersistentStore:&executeError]) {
          NSLog(@"Failed to save to data store");
     }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!