所以我转而使用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
答案 0 :(得分:4)
听起来响应描述符无法匹配所请求的URL。两个想法:
接下来我要尝试使用调试器逐步完成匹配。在RKObjectManager
内,候选响应映射列表由RKFilteredArrayOfResponseDescriptorsMatchingPath
函数构建。如果没有返回预期的响应映射,那么路径模式的请求路径将无法评估。
如果事情看起来不错,那么下一个可能发生不匹配的地方就是RKResponseMapperOperation
方法中的buildResponseMappingsDictionary
。此方法评估针对每个响应描述符的响应。如果响应无法与您的响应描述符匹配,那么您将在此处获得意外结果。
要检查的最终位置是在RKResponseMapperOperation内。这将从匹配的描述符中获取映射并应用它们。 RKMapperOperation main
方法应包含您期望的反序列化响应以及mappingsDictionary
属性上的相应映射。
答案 1 :(得分:0)
即使设置似乎正确,映射也是错误的。映射调用的信息嵌套在给定对象中,因此您必须告诉映射查找对象内部的值。
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。