【问题标题】:RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject classRestKit 0.20 — CoreData:错误:无法调用 NSManagedObject 类上的指定初始化程序
【发布时间】:2012-12-05 23:12:12
【问题描述】:

我遇到了一个我似乎无法解决的新问题...这是我的 RestKit 代码,遵循 Twitter 核心数据示例:

//
// RESTKIT
//

// restkit object manager
NSString *baseURL = @"http://test1.website.com";
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:baseURL]];

// enable activity indicator
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;

// managed object model
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"AppName" withExtension:@"momd"];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
// NSLog(@"managed object model: %@", managedObjectModel);

// managed object store
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
// NSLog(@"managed object store: %@", managedObjectStore);

// persistent store
NSError *error;
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"AppName.sqlite"];
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
if (error) {
    NSLog(@"unresolved error %@, %@", error, [error userInfo]);
    abort();
}
// NSLog(@"persistent store coordinator: %@", managedObjectStore.persistentStoreCoordinator);
// NSLog(@"persistent store: %@", persistentStore);

// managed object contexts
[managedObjectStore createManagedObjectContexts];
// NSLog(@"managed object context: %@", managedObjectStore.mainQueueManagedObjectContext);

// managed object cache
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext];
// NSLog(@"mangaged object cache: %@", managedObjectStore.managedObjectCache);

//
// entity mapping
//

// album entity mapping
RKEntityMapping *albumEntityMapping = [RKEntityMapping mappingForEntityForName:@"Album" inManagedObjectStore:managedObjectStore];
albumEntityMapping.identificationAttributes = @[ @"identifier" ];
[albumEntityMapping addAttributeMappingsFromDictionary:@{ @"title" : @"title" }];

//
// response descriptor
//

// album response descriptor
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:albumEntityMapping pathPattern:[NSString stringWithFormat:@"/albums/display/%@/%@", userInfo[@"userID"], userInfo[@"apiKey"]] keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

我通过 UITableViewController 类从 API 请求信息:

- (void)loadObjectsFromResourcePath:(NSString *)resourcePath;
{
   [[RKObjectManager sharedManager] getObjectsAtPath:resourcePath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        NSLog(@"mapping result: %@", [mappingResult array]);
        [self stopLoading];
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@", error);
        [self stopLoading];
    }];
}

但是,使用此代码会出现以下错误:

2012-12-05 17:08:14.830 AppName Alpha[1310:907] I restkit:RKLog.m:34 RestKit logging initialized...
2012-12-05 17:08:15.107 AppName Alpha[1310:907] resource path: albums/display/102/drrrbSAt6RFLsgHaKiiB6kBIyYRyzxLYinL84Kba
2012-12-05 17:08:15.113 AppName Alpha[1310:907] I restkit.support:RKMIMETypeSerialization.m:115 JSON Serialization class 'RKNSJSONSerialization' detected: Registering for MIME Type 'application/json
2012-12-05 17:08:15.118 AppName Alpha[1310:1603] I restkit.network:RKHTTPRequestOperation.m:141 GET 'http://test1.AppName.com/albums/display/102/drrrbSAt6RFLsgHaKiiB6kBIyYRyzxLYinL84Kba'
2012-12-05 17:08:15.122 AppName Alpha[1310:907] error (null), (null)
2012-12-05 17:08:35.333 AppName Alpha[1310:3e13] I restkit.network:RKHTTPRequestOperation.m:158 GET 'http://test1.AppName.com/albums/display/102/drrrbSAt6RFLsgHaKiiB6kBIyYRyzxLYinL84Kba' (200)
2012-12-05 17:08:35.351 AppName Alpha[1310:1507] CoreData: error: Failed to call designated initializer on NSManagedObject class 'AppName Album' 
2012-12-05 17:08:35.355 AppName Alpha[1310:1507] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AppName 0x1c579380> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "Title".'
*** First throw call stack:
(0x39e503e7 0x38ea9963 0x39e500d5 0x371f8d81 0x3276cc1d 0xbf545 0xc00f5 0xc0ab9 0xc44c5 0x32791299 0xb738b 0xb6e5f 0xb7ca5 0xb8343 0xb895b 0x32791299 0xf96ef 0xf8c4b 0x32791299 0x328097f7 0x32c38793 0x32c3c657 0x32c3c7d9 0x3312a7f1 0x3312a684)
libc++abi.dylib: terminate called throwing an exception

您可以在此处查看返回的 JSON 对象: http://test1.AppName.com/albums/display/102/drrrbSAt6RFLsgHaKiiB6kBIyYRyzxLYinL84Kba

【问题讨论】:

    标签: objective-c restkit restkit-0.20


    【解决方案1】:

    这表明在没有调用适当的 Core Data 初始化程序的情况下尝试创建对象,这可能意味着您获得的是 RKObjectRequestOperation 实例而不是 RKManagedObjectRequestOperation

    我怀疑响应描述符未能与 URL 匹配,这导致它选择了错误的对象请求操作类型。您可以通过在appropriateObjectRequestOperationWithObject:method:path:parameters: 中的以下行放置一个断点来检查:

    NSArray *matchingDescriptors = RKFilteredArrayOfResponseDescriptorsMatchingPath(self.responseDescriptors, requestPath);
    BOOL containsEntityMapping = RKDoesArrayOfResponseDescriptorsContainEntityMapping(matchingDescriptors);
    BOOL isManagedObjectRequestOperation = (containsEntityMapping || [object isKindOfClass:[NSManagedObject class]]);
    

    这个逻辑负责选择创建的操作类型。检查matchingDescriptors 是否包含您期望的响应描述符,然后检查接下来的两个布尔值。我的猜测是 RKFilteredArrayOfResponseDescriptorsMatchingPath 没有返回您期望的结果。

    【讨论】:

    • 这是什么类?另外,在第一个 GET 请求上返回 error(null, null) 的内容。另外,为什么有两个请求出去? :)
    • appropriateObjectRequestOperationWithObject:method:path:parameters: 在 RKObjectManager 上。请注意,没有 2 个请求发出。第一条日志消息用于发送,第二条日志消息包含 (200) 在响应中。 'error(null, null)' 消息来自失败完成块中的 NSLog。
    • 我现在强制执行 RKManagedObjectRequestOperation 操作,它正在工作。明天我将不得不回溯,看看问题的根源是什么。错误(null,null)不是来自我。我甚至将块注释掉以进行仔细检查。
    • 我想补充一点,如果您使用RKObjectMapping 而不是RKEntityMapping 初始化RKResponseDescriptor,也会发生此错误。我盲目地遵循教程,没有意识到我不必为我的请求和响应描述符创建单独的RKObjectMapping
    • 为了呼应上面的许多其他内容,当RKResponseDescriptor 上的路径不匹配时会发生这种情况。 URL 类似于/abc/objects,但我只在描述符中包含了“/objects”。谢谢!!
    【解决方案2】:

    现在已阅读 RKObjectManager 的类文档,其中阐明了基本 url 的 AF 网络处理。如果我先读到这篇文章,会为自己节省一些时间,并且还可以避免在 RestKit 示例中创建与路径无关的 cmets。

    在匹配对象和实体映射的混合时遇到问题,包括具有 nil 键路径和不同 URL 的多个对象映射,并且发现此答案对于确定以下描述的问题的原因非常有用,这可能有助于其他遇到类似问题的人问题。

    【讨论】:

      猜你喜欢
      • 2013-08-17
      • 1970-01-01
      • 2014-10-19
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      相关资源
      最近更新 更多