0

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]; }];

Luda
  • 7,282
  • 12
  • 79
  • 139
  • You shouldn't be using `persistentStoreManagedObjectContext` in the completion callback. Certainly without using the block based API. You also need to confirm what cache options you're using to allow duplicate location. And turn on trace logging and see what it says about locating dupes. – Wain Sep 21 '14 at 11:21
  • @Wain, didn't understand what I should and should not do, accept of not using NSManagedObjectContext. But the way if I am not calling [moc save] will the data be saved to core data? – Luda Sep 21 '14 at 11:40
  • RestKit will save automatically, but any changes you make in the completion block or later need to be saved explicitly. You need to use the main MOC or a block API method to do that though. There isn't enough info in the question to know where your dupes are from currently. Trace logging should help. – Wain Sep 21 '14 at 14:55
  • @Wain 1. What is Trace logging and how can that help me? 2. I don't really understand how to make restkit understand for itself that set of Apps objects received from the server needs to be connected to Me object in Core Data. After all they are created in different classes. 3. And how to tell RestKit that he needs to fetch more data for already existing objects (App) from another url – Luda Sep 22 '14 at 07:47
  • They aren't in different classes, they are both in `COApp`. Google for RestKit trace logging and foreign key mapping. You need to explicitly request the additional data from the other URL (object manager). – Wain Sep 22 '14 at 08:11
  • Hi @Wain, I found this: http://stackoverflow.com/questions/17326087/foreign-key-relationship-mapping-with-restkit. According to this and to common sense, relationship is something that goes between 2 objects and here I have 1 objects, the COApp. So it is still not clear. BTW, I turned on the trace logging. Thank you – Luda Oct 02 '14 at 08:24
  • Relationships are usually between 2 entity types, but not necessarily. Consider a `Person` entity which has a relationship to itself so that a person can have a `manager`. The relationship always connects 2 object instances though. You don't want foreign key mapping, you need to have a fetch request cache (I'm not sure an in-memory one will work) attached to each object manager and unique identity attributes specified on all mappings. Show how you're configuring your object managers. – Wain Oct 02 '14 at 08:33
  • @Wain It is not even relationship between two instances of one class. It is the same instance that gets its data from 2 deferent services. 1 services brings me a list of item ids, the other services brings me the details of these items. – Luda Oct 02 '14 at 08:36
  • That's why I said you don't want foreign key mapping, you need to review your managed object store and cache configuration. Please show the code where you create and configure the object managers and core data stack. – Wain Oct 02 '14 at 08:51
  • did you solve this problem? I have one global rkobjectmanager configured with my restful service's base URL and conditions. I have a local rkobjectmanager configured with a different site's URL and I get multiple objects created when they should be updating – CQM Jan 11 '16 at 18:15

0 Answers0