Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restkit creates duplicates (error: "Managed object cache returned 2 objects") only on device, not in simulator

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!

like image 637
Ralf Hundewadt Avatar asked Feb 01 '26 13:02

Ralf Hundewadt


1 Answers

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.

like image 197
Ralf Hundewadt Avatar answered Feb 04 '26 06:02

Ralf Hundewadt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!