【问题标题】:"Unable to find any mappings for the given content, keyPath=null" RestKit 0.2“找不到给定内容的任何映射,keyPath=null”RestKit 0.2
【发布时间】:2012-11-16 14:40:47
【问题描述】:

所以我转而使用 RestKit 0.2 和 CoreData,但在尝试正确映射时遇到了很多麻烦……我不明白为什么。我的服务器的 JSON 响应是这样的:

{
"meta": 
   {
   "limit": 20, 
   "next": null, 
   "offset": 0, 
   "previous": null, 
   "total_count": 2
   }, 
   "objects": 
   [{
       "creation_date": "2012-10-15T20:16:47", 
       "description": "", 
       "id": 1, 
       "last_modified": 
       "2012-10-15T20:16:47", 
       "order": 1, 
       "other_names": "", 
       "primary_name": "Mixing",
       "production_line": "/api/rest/productionlines/1/", 
       "resource_uri": "/api/rest/cells/1/"
   }, 
   {
       "creation_date": "2012-10-15T20:16:47", 
       "description": "",
       "id": 2, 
       "last_modified": "2012-10-15T20:16:47",
       "order": 2, "other_names": "", 
       "primary_name": "Packaging", 
       "production_line": "/api/rest/productionlines/1/",
       "resource_uri": "/api/rest/cells/2/"
   }]
}

然后在 XCode 中我有:

RKObjectManager *objectManager = [RKObjectManager sharedManager];

[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;


RKEntityMapping *cellMapping = [RKEntityMapping mappingForEntityForName:@"Cell" inManagedObjectStore:managedObjectStore];
cellMapping.primaryKeyAttribute = @"identifier";
[cellMapping addAttributeMappingsFromDictionary:@{
 @"id": @"identifier",
 @"primary_name": @"primaryName",
 }];



RKResponseDescriptor *responseCell = [RKResponseDescriptor responseDescriptorWithMapping:cellMapping
                                                                             pathPattern:@"/api/rest/cells/?format=json"
                                                                                 keyPath:@"objects"
                                                                             statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


[objectManager addResponseDescriptorsFromArray:@[responseCell, responseUser, responseCompany]];


[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"AppDB.sqlite"];
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"SeedDatabase" ofType:@"sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];

// Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];

我的要求是:

    [[RKObjectManager sharedManager] getObjectsAtPath:@"/api/rest/cells/?format=json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        RKLogInfo(@"Load complete: Table should refresh...");

        [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"LastUpdatedAt"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        RKLogError(@"Load failed with error: %@", error);
    }];

而且我总是收到以下错误:

**Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "Unable to find any mappings for the given content" UserInfo=0x1102d500 {DetailedErrors=(), NSLocalizedDescription=Unable to find any mappings for the given content, keyPath=null}**

非常感谢!

更新:我添加了 cellMapping.forceCollectionMapping = YES; 但仍然没有运气:(!

更新 #2:按照 Blake 的建议,我尝试更改路径并且成功了!我做了 /api/rest/cells/ 而不是 /api/rest/cells/?format=json 并且我的服务器返回了所有内容并且映射成功!

现在我得到的唯一问题是以下错误:

2012-11-21 14:48:49.414 App[3125:617] W restkit.object_mapping:RKMapperOperation.m:176 强制集合映射但可映射对象的类型为“__NSCFArray”而不是 NSDictionary

【问题讨论】:

    标签: ios6 restkit


    【解决方案1】:

    听起来响应描述符与请求的 URL 不匹配。两个想法:

    1. 尝试完全删除路径模式(传入 nil)并仅在“单元格”上使用基于键路径的匹配
    2. 尝试使用“/api/rest/cells”的路径模式

    接下来我会尝试使用调试器来逐步完成匹配。在RKObjectManager 中,候选响应映射列表由RKFilteredArrayOfResponseDescriptorsMatchingPath 函数构建。如果那里没有返回您预期的响应映射,则路径模式的请求路径无法评估。

    如果那里看起来不错,下一个可能发生不匹配的地方是RKResponseMapperOperation 中的buildResponseMappingsDictionary 方法。此方法根据每个响应描述符评估响应。如果响应未能与您的响应描述符匹配,那么您将在这里得到意想不到的结果。

    最后检查的地方是 RKResponseMapperOperation。这从匹配的描述符中获取映射并应用它们。 RKMapperOperation main 方法应该包含您期望的反序列化响应和mappingsDictionary 属性上的适当映射。

    【讨论】:

    • 我听从了您的建议,然后删除了我拥有的关于力映射的行并且它有效!再次感谢布莱克!
    【解决方案2】:

    即使设置看起来正确,映射也是错误的。您的映射调用的信息嵌套在给定的对象中,因此您必须告诉映射查找对象内部的值。

    RKEntityMapping *cellMapping = [RKEntityMapping mappingForEntityForName:@"Cell" inManagedObjectStore:managedObjectStore];
    cellMapping.primaryKeyAttribute = @"identifier";
    
    [cellMapping mapKeyOfNestedDictionaryToAttribute:@"objects"];
    [cellMapping mapFromKeyPath:@".id" toAttribute:"identifier"];
    [cellMapping mapFromKeyPath:@"primary_name" toAttribute:"primaryName"];
    

    更多信息请查看RKObjectMapping Reference

    【讨论】:

    • 问题是函数 mapFromKeyPath 仅在 RKObjectMapping 中......我正在使用 CoreData。因此,我有一个 RKEntityMapping 并且 mapFromKeyPath 不在 RKEntityMapping 中。
    • 下周我将更新到 RestKit 0.20,所以我会告诉你我发现的关于在那里配置映射的过程。我的建议:如果项目紧急,请使用 master 分支(10.3),因为 0.20 正在大力开发。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多