Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 仅用于对象映射的RestKit_Objective C_Restkit - Fatal编程技术网

Objective c 仅用于对象映射的RestKit

Objective c 仅用于对象映射的RestKit,objective-c,restkit,Objective C,Restkit,我使用RestKit将数据从api映射到CoreData实体,但当我想以非标准方式使用此库时,遇到了一些困难。例如,如中所述 所以我决定使用AFNetworking+MagicalRecord并为自己做一些工作。我喜欢RestKit对象映射,所以我想使用它 [afhttpRSClient getPath:@"_api/items" parameters:parameters success:^(AFHTTPRequestOperation

我使用RestKit将数据从api映射到CoreData实体,但当我想以非标准方式使用此库时,遇到了一些困难。例如,如中所述

所以我决定使用AFNetworking+MagicalRecord并为自己做一些工作。我喜欢RestKit对象映射,所以我想使用它

[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。其他问题的答案告诉您如何提取“额外”信息