Objective c 仅用于对象映射的RestKit
我使用RestKit将数据从api映射到CoreData实体,但当我想以非标准方式使用此库时,遇到了一些困难。例如,如中所述 所以我决定使用AFNetworking+MagicalRecord并为自己做一些工作。我喜欢RestKit对象映射,所以我想使用它Objective c 仅用于对象映射的RestKit,objective-c,restkit,Objective C,Restkit,我使用RestKit将数据从api映射到CoreData实体,但当我想以非标准方式使用此库时,遇到了一些困难。例如,如中所述 所以我决定使用AFNetworking+MagicalRecord并为自己做一些工作。我喜欢RestKit对象映射,所以我想使用它 [afhttpRSClient getPath:@"_api/items" parameters:parameters success:^(AFHTTPRequestOperation
[afhttpRSClient getPath:@"_api/items"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([responseObject isKindOfClass:[NSDictionary class]]) {
id items = responseObject[@"items"];
if (items && [items isKindOfClass:[NSArray class]]) {
NSManagedObjectContext *context = [NSManagedObjectContext MR_contextForCurrentThread];
for (NSDictionary *item in items) {
SomeEntity *entity = [SomeEntity MR_findFirstByAttribute:@"entityId" withValue:item[@"id"] inContext:context];
if (!entity) {
entity = [SomeEntity MR_createInContext:context];
}
RKMappingOperation *mappingOperation;
mappingOperation = [[RKMappingOperation alloc] initWithSourceObject:item
destinationObject:entity
mapping:[SomeEntity entityMapping]];
NSError *mappingError = nil;
BOOL mappingSuccess = [mappingOperation performMapping:&mappingError];
}
}
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", error);
}];
但是我想知道有没有合适的方法可以使用RestKit的部分来自动将我的
项NSArray直接绑定到CoreData?我认为它已经实现了这段代码的更多公共部分,我想使用它。可能吗?您可以使用RKMappingOperation
执行您想要的操作,主要是缺少所需的数据源(请参见RKManagedObjectMappingOperationDataSource
)。通常,您不会提供目标实体,而是允许dataSource
创建它(基于映射实体类型)
通常情况下,最好使用RestKit来处理希望执行映射的请求,而在不需要映射时使用AFN。其他问题的答案告诉您如何提取“额外”信息