我很难将 JSON 映射到我的 RestKit 应用程序中。我也想将它保存在核心数据中。因此这里有两个图像链接。 第一张图片,我的 JSON 是什么样子的。
第二张图片是我的核心数据库的样子。
现在,我首先制作了没有核心数据层的应用程序。因此,只需读取数据并使用它即可。那时一切都正常了。但使用核心数据时,我遇到了一些像这样的映射错误。
日志
2013-01-10 10:46:02.663 Offitel2[27978:6813] D restkit.object_mapping:RKMappingOperation.m:635 Did not find mappable relationship value keyPath 'Person'
2013-01-10 10:46:02.664 Offitel2[27978:6813] D restkit.object_mapping:RKMappingOperation.m:635 Did not find mappable relationship value keyPath 'Function'
2013-01-10 10:46:02.664 Offitel2[27978:6813] D restkit.object_mapping:RKMappingOperation.m:635 Did not find mappable relationship value keyPath 'Department'
2013-01-10 10:46:02.665 Offitel2[27978:6813] D restkit.object_mapping:RKMappingOperation.m:635 Did not find mappable relationship value keyPath 'Company'
2013-01-10 10:46:02.672 Offitel2[27978:164f] W restkit.core_data:RKManagedObjectRequestOperation.m:555 Caught undefined key exception for keyPath 'user' in mapping result: This likely indicates an ambiguous keyPath is used across response descriptor or dynamic mappings.
2013-01-10 10:46:02.673 Offitel2[27978:164f] W restkit.core_data:RKManagedObjectRequestOperation.m:555 Caught undefined key exception for keyPath 'function' in mapping result: This likely indicates an ambiguous keyPath is used across response descriptor or dynamic mappings.
2013-01-10 10:46:02.674 Offitel2[27978:164f] W restkit.core_data:RKManagedObjectRequestOperation.m:555 Caught undefined key exception for keyPath 'department' in mapping result: This likely indicates an ambiguous keyPath is used across response descriptor or dynamic mappings.
2013-01-10 10:46:02.676 Offitel2[27978:164f] W restkit.core_data:RKManagedObjectRequestOperation.m:555 Caught undefined key exception for keyPath 'company' in mapping result: This likely indicates an ambiguous keyPath is used across response descriptor or dynamic mappings.
这就是我在代码中进行关系映射的方式。
RKRelationshipMapping* relationUserMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Person"toKeyPath:@"user"withMapping:personMapping];
RKRelationshipMapping* relationFunctionMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Function"toKeyPath:@"function"withMapping:functionMapping];
RKRelationshipMapping* relationDepartmentMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Department"toKeyPath:@"department"withMapping:departmentMapping];
RKRelationshipMapping* relationCompanyMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Company"toKeyPath:@"company"withMapping:companyMapping];
NSLog(@"till here7");
[dataMapping addPropertyMapping:relationUserMapping];
[dataMapping addPropertyMapping:relationFunctionMapping];
[dataMapping addPropertyMapping:relationDepartmentMapping];
[dataMapping addPropertyMapping:relationCompanyMapping];
任何人都可以帮我解决这个问题吗?我现在在核心数据的映射上苦苦挣扎了好几天。
亲切的问候
最佳答案
尝试使用 RKManagedObjectMapping 来实现此目的。配置 RKManagedObjectMapping 对象如下。
RKURL *baseURL = [RKURL URLWithBaseURLString:serverUrl];
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL];
objectManager.client.baseURL = baseURL;
objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL];
RKManagedObjectStore *objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:DB_NAME];
objectManager.objectStore = objectStore;
然后,您必须为每个数据类及其所具有的关系设置映射。我只是尝试在这里放一个我用过的例子。
/** specify the base mapping of reservation class */
RKManagedObjectMapping *getReservationMapping = [RKManagedObjectMapping mappingForClass:[Reservation class] inManagedObjectStore:objectManager.objectStore];
/** generate mapping for Reservation class */
[self setReservationMapping:getReservationMapping];
getReservationMapping.primaryKeyAttribute=@"reservationId";
[objectManager.mappingProvider setMapping:getReservationMapping forKeyPath:@""];
/** specify the base mapping of ReservationsType class */
RKManagedObjectMapping *resTypeMapping = [RKManagedObjectMapping mappingForClass:[ReservationsType class] inManagedObjectStore:objectManager.objectStore];
/** generate mapping for ReservationsType class */
[self setReservationsTypeMapping:resTypeMapping];
resTypeMapping.primaryKeyAttribute=@"reservationTypeId";
[getReservationMapping mapRelationship:@"reservationsType" withMapping:resTypeMapping];
[objectManager.mappingProvider setMapping:resTypeMapping forKeyPath:@".reservationsType"];
我的 setReservationMapping 如下
- (void)setReservationMapping:(RKManagedObjectMapping *)object
{
[object mapKeyPathsToAttributes:
@"id", @"reservationId",
@"comment", @"comment",
@"date", @"date",
@"email", @"email",nil];
}
这是我的 JSON 示例。请注意,我的预留节点没有标识符,它是空白的,因此我给出了 keyPath:@""。
[{
id: 5644,
comment: "",
email: "danish@abc.com",
firstName: "Danny",
reservationsType:
{
id: 1,
name: "Reservation",
}
}]
希望对你有帮助。
关于ios - 在 Restkit 中映射 JSON 很困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14255008/