ios - 使用 RestKit 0.20 初始化核心数据

标签 ios objective-c core-data restkit restkit-0.20

我正在尝试将使用 Restkit 0.1 的现有应用程序更新到 0.20,但在设置 Core Data Stack 时遇到了一些问题,因为它遇到了异常。

我的 AppDelegate 上有以下代码:

     /*
     Complete Core Data stack initialization
     */
    NSError *error = nil;
    NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"AppModel" ofType:@"momd"]];
    // NOTE: Due to an iOS 5 bug, the managed object model returned is immutable.
    NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
    RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
    [managedObjectStore createPersistentStoreCoordinator];
    NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"TSI.sqlite"];
    NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"];
    //NSPersistentStore __unused *persistentStore = [managedObjectStore addInMemoryPersistentStore:&error];
    NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error];
    NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
    /*
     RESTKIT
    */
    // Create the managed object contexts
    [managedObjectStore createManagedObjectContexts];
     managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];

然后我的映射按照以下方式在 TableViewController 中设置:

    RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];
    NSManagedObjectContext* context = [managedObjectStore mainQueueManagedObjectContext];

    NSString *rootKeyPath;
    rootKeyPath = @"body";
    NSString *endpoint = @"account/login.json";
    RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
    [entityMapping addAttributeMappingsFromDictionary:@{
     @"api_token":@"token",
     @"email":   @"email",
     @"first_name":  @"name",
     @"last_name":   @"surname"
     }];
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping pathPattern:endpoint keyPath:rootKeyPath statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[TSI_SERVER stringByAppendingString:endpoint]]];
    RKManagedObjectRequestOperation *managedObjectRequestOperation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
    //managedObjectRequestOperation.managedObjectContext = self.managedObjectContext;
    [[NSOperationQueue currentQueue] addOperation:managedObjectRequestOperation];

但是每当我在加载 View 后尝试检索数据时,我都会在控制台中收到以下错误(出于安全原因,我屏蔽了我的应用程序使用的名称和服务器):

2013-04-16 14:41:50.576 <<MyApp>>[4525:907] I restkit:RKLog.m:34 RestKit logging initialized...
2013-04-16 14:41:51.009 <<MyApp>>[4525:907] [HEATMA.PS] Running Heatma.ps version 4.0.2. For help go to www.heatma.ps/support 
2013-04-16 14:42:54.464 <<MyApp>>[4525:3907] *** Assertion failure in
-[RKManagedObjectResponseMapperOperation performMappingWithObject:error:], /Users/manuel_matute/Code/RestKit/Code/Network/RKResponseMapperOperation.m:358 
2013-04-16 14:42:59.876 <<MyApp>>[4525:3907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to perform mapping: No `managedObjectContext` assigned. (Mapping response.URL = http://www.<<myserver>>.com/rest/account/login.json)'
 *** First throw call stack: (0x32a7b2a3 0x3a7a397f 0x32a7b15d 0x33350ab7 0xe37e1 0xe1b97 0x333085c1 0x33380be3 0x3abbb11f 0x3abbf961 0x3abbfac1 0x3abefa11 0x3abef8a4) libc++abi.dylib: terminate called throwing an exception

有没有人知道可能是什么问题?

谢谢!

最佳答案

解决了!

看起来我需要明确地将上下文分配给 managedObjectRequestOperation 为:

managedObjectRequestOperation.managedObjectContext = context;

关于ios - 使用 RestKit 0.20 初始化核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16038834/

相关文章:

objective-c - iOS 使用 key 标识符对 NSData 进行加密

iOS - 对象未添加到数组

ios - 使用图像在 objective-c 中创建 PDF 表单

ios - NSPersistentContainer/核心数据/只读存储

ios UIImageRenderingModeAlwaysTemplate 不会忽略不透明度

iphone - 在 ARC 下收到内存警告时,我应该将强 IBOutlet 设置为 nil 吗?

ios - 同一个 UIViewController 中的两个 UITableView

ios - 禁用 GPUImageMovieWriter 上的所有音频

ios - 如何用核心数据实现表部分

ios - NSURLSession 辅助类