Reputation: 1263
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?
Upvotes: 1
Views: 140
Reputation: 119021
You can do what you want with RKMappingOperation
, mainly you're missing the dataSource
which is required (see RKManagedObjectMappingOperationDataSource
). Generally you wouldn't supply the destination entity, you would allow the dataSource
to create it (based on the mapping entity type).
You're generally better off using RestKit to process requests where you want to perform mapping and using AFN when you don't need mapping. The other question answers tell you how to extract the 'extra' information.
Upvotes: 2