I am using RestKit for mapping data from my api to CoreData entities but i encountered with some difficulties when i want to use this library in not standard way. For example described in RestKit additional data in response
So i decide to use AFNetworking + MagicalRecord and make some work for myself. I like RestKit object mapping so i want to use it.
[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);
}];
But i wonder is there any proper way to use parts of RestKit to automatically bind my items NSArray directly to CoreData? I think it's have already implemented more common parts of this code and i want to use it. Does it possible?