3
votes

I get the warning "restkit.core_data:RKManagedObjectMappingOperationDataSource.m:264 Managed object cache returned 2 objects for the identifier configured for the 'MyEntity' entity, expected 1." only on my device, but never in the simulator.

The sqlite database in the simulator is fine, but on the device I have identical, duplicate entries. If I delete the database, the warning pops up again after a while. But only on the device.

I'm using Restkit 0.23.1 to import JSON data to core data with deleting orphaned objects. The identificationAttributes are set.

Any ideas, if there is a difference between the simulator and the device that may cause this problem?

Thanks for help.

EDIT

I get the error for various entities. This is my mapping for the entity ResellerType (I have only one mapping for each entity):

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"ResellerType"
                                               inManagedObjectStore:[YPIDataProvider sharedDataProvider].objectManager.managedObjectStore];
[mapping addAttributeMappingsFromArray:@[ @"resellerTypeId",
                                          @"resellerTypeName",
                                          @"position" ]];
mapping.identificationAttributes = @[ @"resellerTypeId" ];

I have a To Manyrelationship to the Reseller table and a Many To Manyrelationship to my MetaData table.

This is my RestKit setup:

NSError *error = nil;

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:kDataModelName withExtension:@"momd"];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

self.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

[self.managedObjectStore createPersistentStoreCoordinator];

[self.managedObjectStore
 addSQLitePersistentStoreAtPath:self.pathToDatabase
 fromSeedDatabaseAtPath:nil
 withConfiguration:nil
 options:@{ NSInferMappingModelAutomaticallyOption: @YES,
            NSMigratePersistentStoresAutomaticallyOption: @YES }
 error:&error];

[self.managedObjectStore createManagedObjectContexts];

// managedObjectCache: Configure a managed object cache to ensure we do not create duplicate objects:
NSManagedObjectContext *moc = self.managedObjectStore.persistentStoreManagedObjectContext;
self.managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc]
                                              initWithManagedObjectContext:moc];

I delete the NSURLCache before I send a request to the server ([[NSURLCache sharedURLCache] removeAllCachedResponses]). Can you find anything wrong here?

Thanks for reading all this!

1
There shouldn't be. Is it always the same type of object? What patterns do you notice? What mappings are associated? Do all mappings for the same entity have the same identification attributes? - Wain
Did you define your primary key correctly? e.g.: [_mapping setIdentificationAttributes:@[ @"objectId" ]]; whereas objectId is you primary key on the core data object. - electronix384128
I updated my question with some more details. - Ralf Hundewadt

1 Answers

1
votes

I found the answer for this in issue #1613 for RestKit: https://github.com/RestKit/RestKit/issues/1613 If a memory warning happens, RestKit flushes the cache. If a mapping operation is in progress, you may have duplicate data afterwards.

I deleted [self flush:nil]; in didReceiveMemoryWarning in RKEntityByAttributeCache.m and the problem disappeared.