【发布时间】:2014-03-24 16:37:57
【问题描述】:
如何从RKMappingResult 获取特定类型的对象?
我需要建立从RKMappingResults 中的映射数据到现有对象的关系。
我试过了:
[self getObjectsAtPath:[NSString stringWithFormat:@"%@%@", BASEURL restaurant.remoteID.stringValue] parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
if (!mappingResult.array || !mappingResult.array.count) {
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error = [self requestFailed];
if (block) {
block(nil, error);
}
}) ;
} else {
__block BOOL done = NO;
[[[CoreDataManager sharedInstance] backgroundManagedObjectContext] performBlockAndWait:^{
for (Menu *menu in mappingResult.array) {
[restaurant addMenuObject:menu];
}
for (TableIds *tableId in mappingResult.array) {
[restaurant addTableIdsObject:tableId];
NSLog(@"%@", tableId);
}
[[CoreDataManager sharedInstance] saveBackgroundContext];
done = YES;
}];
if (done) {
dispatch_async(dispatch_get_main_queue(), ^{
if (block) {
block(mappingResult, nil);
}
});
}
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (block) {
block(nil, error);
}
}) ;
}];
在 for 循环中,我尝试获取已映射的两种类型的对象,但由于某种原因,它似乎无法找到 TableId 和 Menu 之间的区别。因此,一些菜单被添加为 tableId 并导致应用程序崩溃。
当我例如NSLog 所有TableIds 时收到的内容:
<Menu: 0x20e4dda0> (entity: Menu; id: 0xf19eec0 <x-coredata://9F31F549-BE4D-434C-935F-F689839989F2/Menu/p39> ; data: { ......
如何获取正确的对象并正确建立关系?
【问题讨论】:
标签: ios objective-c core-data restkit restkit-0.20