Ios6 &引用;找不到给定内容的任何映射,keyPath=null";RestKit 0.2
所以我转而使用RESTKit0.2和CoreData,我在尝试正确映射时遇到了很多麻烦。。。我不明白为什么。我的服务器的JSON响应如下:Ios6 &引用;找不到给定内容的任何映射,keyPath=null";RestKit 0.2,ios6,restkit,Ios6,Restkit,所以我转而使用RESTKit0.2和CoreData,我在尝试正确映射时遇到了很多麻烦。。。我不明白为什么。我的服务器的JSON响应如下: { "meta": { "limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 2 }, "objects": [{ "creation_date": "2012-10-15T20:16:
{
"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-2114:48:49.414 App[3125:617]W restkit.object_-mapping:RKMapperOperation.m:176强制集合映射,但可映射对象的类型为“u-NSCFArray”,而不是NSDictionary即使设置似乎正确,映射也是错误的。映射调用的信息嵌套在给定对象中,因此您必须告诉映射查找该值它位于对象的内部
RKEntityMapping *cellMapping = [RKEntityMapping mappingForEntityForName:@"Cell" inManagedObjectStore:managedObjectStore];
cellMapping.primaryKeyAttribute = @"identifier";
[cellMapping mapKeyOfNestedDictionaryToAttribute:@"objects"];
[cellMapping mapFromKeyPath:@".id" toAttribute:"identifier"];
[cellMapping mapFromKeyPath:@"primary_name" toAttribute:"primaryName"];
有关详细信息,请检查。即使设置似乎正确,映射也是错误的。映射调用的信息嵌套在给定对象中,因此您必须告诉映射查找对象内部的值
RKEntityMapping *cellMapping = [RKEntityMapping mappingForEntityForName:@"Cell" inManagedObjectStore:managedObjectStore];
cellMapping.primaryKeyAttribute = @"identifier";
[cellMapping mapKeyOfNestedDictionaryToAttribute:@"objects"];
[cellMapping mapFromKeyPath:@".id" toAttribute:"identifier"];
[cellMapping mapFromKeyPath:@"primary_name" toAttribute:"primaryName"];
有关更多信息,请查看。听起来响应描述符与请求的URL不匹配。有两个想法:
RKObjectManager
中,候选响应映射列表由RKFilteredArrayOfResponseDescriptorsMatchingPath
函数生成。如果未在其中返回预期的响应映射,则路径模式的请求路径为未能评估
如果情况看起来不错,下一个可能出现不匹配的地方是buildResponseMappingsDictionary
方法中的RKResponseMapperOperation
。此方法根据每个响应描述符评估响应。如果响应未能与响应描述符匹配,则会得到意外的结果他在这儿
最后一个要检查的地方是RKResponseMapperOperation。它从匹配的描述符中获取映射并应用它们。RKMapperOperation
main
方法应该包含您期望的反序列化响应以及mappingsDictionary
属性上的相应映射。听起来像是响应描述iptor无法匹配请求的URL。有两个想法:
RKObjectManager
中,候选响应映射列表由RKFilteredArrayOfResponseDescriptorsMatchingPath
函数生成。如果未在其中返回预期的响应映射,则路径模式的请求路径为未能评估
如果情况看起来不错,下一个可能出现不匹配的地方是buildResponseMappingsDictionary
方法中的RKResponseMapperOperation
。此方法根据每个响应描述符评估响应。如果响应未能与响应描述符匹配,则会得到意外的结果他在这儿
最后要检查的地方是RKResponseMapperOperation。它从匹配的描述符中获取映射并应用它们。RKMapperOperation
main
方法应该包含您期望的反序列化响应以及mappingsDictionary
属性上的适当映射。问题是函数mapFromKeyPath仅在RKObjectMapping中…我使用的是CoreData。因此,我有一个RKEntityMapping,mapFromKeyPath不在RKEntityMapping中。下周我将更新到RestKit 0.20,所以我会告诉您我在那里发现的有关配置映射过程的信息。我的建议:使用主分支(10.3)如果项目很紧急,因为0.20正在进行大量开发。问题是mapFromKeyPath函数只在RKObjectMapping中…我正在使用CoreData。因此,我有一个RKEntityMapping,而mapFromKeyPath不在RKEntityMapping中。我将在下周更新到RestKit 0.20,因此我会让您知道我对配置过程的了解给那边的映射打分。我的建议是:使用主分支(10.3)如果项目很紧急,因为0.20正在进行大量开发。我听从了你的建议,然后删除了关于力映射的行,它成功了!再次感谢Blake!我听从了你的建议,然后删除了关于力映射的行,它成功了!再次感谢Blake!