I have 2 RKObjectManager that configured to use same managedObjectStore.
With the first RKObjectManager I fetch list of objects, and with the second RKObjectManager I want to add data to these objects.
But instead of updating existing objects, new ones are created.
What am I missing?
Here is my code:
RKEntityMapping* serverAppMapping = [RKEntityMapping mappingForEntityForName:@"COApp" inManagedObjectStore:[RKObjectManager coopsiManager].managedObjectStore];
[serverAppMapping addAttributeMappingsFromDictionary:@{
@"id": @"coID"
}];
serverAppMapping.identificationAttributes = @[ @"coID" ];
RKResponseDescriptor *serverAppResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:serverAppMapping method:RKRequestMethodGET pathPattern:@"/me/apps" keyPath:@"data.items" statusCodes:[NSIndexSet indexSetWithIndex:200]];
RKEntityMapping *appsMapping = [RKEntityMapping mappingForEntityForName:@"COApp" inManagedObjectStore:[RKObjectManager coopsiManager].managedObjectStore];
[appsMapping addAttributeMappingsFromDictionary:@{
@"trackName": @"name",
@"trackId": @"coID",
@"artworkUrl60":@"imageURL"}];
appsMapping.identificationAttributes = @[ @"coID" ];
[serverAppMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"app"
toKeyPath:@"app"
withMapping:appsMapping]];
// register mappings with the provider using a response descriptor
RKResponseDescriptor *appResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:appsMapping method:RKRequestMethodGET pathPattern:@"/th/lookup" keyPath:@"results" statusCodes:[NSIndexSet indexSetWithIndex:200]];
[[RKObjectManager appleManager] addResponseDescriptor:appResponseDescriptor];
[[RKObjectManager coopsiManager] addResponseDescriptor:serverAppResponseDescriptor];
NSString* access_token = [[COAuthManager sharedInstance] getUserAccessToken];
NSDictionary *queryParams;
queryParams = [NSDictionary dictionaryWithObjectsAndKeys:access_token, @"access_token", nil];
[[RKObjectManager coopsiManager] getObjectsAtPath:@"/me/apps" parameters:queryParams success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSOrderedSet* set = [NSOrderedSet orderedSetWithArray:mappingResult.array];
[me addNewApps:set];
self.objects = [me.newApps mutableCopy];
[self.delegate dataRefrashed];
NSManagedObjectContext *moc = [[RKObjectManager coopsiManager]managedObjectStore].persistentStoreManagedObjectContext;
NSError *error;
if (![moc save:&error]) {
NSLog(@"Fail");
}else{
NSLog(@"luda rest kit objetcs - %@",me.newApps);
}
for (COApp * app in self.objects) {
NSDictionary *queryParams = [NSDictionary dictionaryWithObjectsAndKeys:app.coID, @"id", nil];
[[RKObjectManager appleManager] getObjectsAtPath:[NSString stringWithFormat:@"/th/lookup"] parameters:queryParams success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
// app.app = mappingResult.array[0]; [self.delegate dataRefrashed]; } failure:^(RKObjectRequestOperation *operation, NSError *error) { [self presentError:error]; }]; } } failure:^(RKObjectRequestOperation *operation, NSError *error) { [self presentError:error]; }];