21565

RESKit: Duplicate objects are created

Question:

Duplicates appear in the database after fetching.

Below are the mapping and the fetch code:

+(RKEntityMapping *)userMapping:(RKEntityMapping *)userMapping { [userMapping addAttributeMappingsFromDictionary:@{ @"userid" : @"userid", @"firstName" : @"firstName", @"lastName" : @"lastName", }]; userMapping.identificationAttributes = @[@"userid"]; return userMapping; }

Fetch Code:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; self.objectManager = [self getObjectManager]; self.objectManager.managedObjectStore = appDelegate.managedObjectStore; self.managedObjectContext = self.objectManager.managedObjectStore.mainQueueManagedObjectContext; [RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON; RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:self.objectManager.managedObjectStore]; userMapping = [RESTMappingProvider userMapping:userMapping]; NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping method:RKRequestMethodGET pathPattern:nil keyPath:@"findUser" statusCodes:statusCodeSet]; NSMutableURLRequest *request = [self.objectManager.HTTPClient requestWithMethod:@"GET" path:@"/findUser" parameters:@{@"userid": userSearch }]; //request.cachePolicy = NSURLRequestReloadIgnoringCacheData; [self.objectManager.HTTPClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]]; operation.managedObjectContext = self.objectManager.managedObjectStore.mainQueueManagedObjectContext; operation.savesToPersistentStore = NO; [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { NSError *error; for (User *foundUser in mappingResult.array) { foundUser.lastUpdated = [NSDate date]; } if ([self.managedObjectContext saveToPersistentStore:&error]) { NSLog (@"******* User OBJECTS SAVED **********"); } } failure:^(RKObjectRequestOperation *operation, NSError *error) { }); }]; [operation start]; [operation waitUntilFinished]; } }

LOG:

2014-07-27 14:25:48.756 App[36854:5b3b] W restkit.core_data:RKManagedObjectMappingOperationDataSource.m:243 Performing managed object mapping with a nil managed object cache: Unable to update existing object instances by identification attributes. Duplicate objects may be created. 2014-07-27 14:25:48.756 App[36854:3803] D restkit.object_mapping:RKPropertyInspector.m:130 Cached property inspection for Class 'User': { email = { isPrimitive = 0; keyValueCodingClass = NSString; name = email; }; firstName = { isPrimitive = 0; keyValueCodingClass = NSString; name = firstName; }; lastName = { isPrimitive = 0; keyValueCodingClass = NSString; name = lastName; }; lastUpdated = { isPrimitive = 0; keyValueCodingClass = NSDate; name = lastUpdated; }; userid = { isPrimitive = 0; keyValueCodingClass = NSString; name = userid; }; } 2014-07-27 14:25:48.757 App[36854:5b3b] D restkit.object_mapping:RKMapperOperation.m:231 Asked to map source object { firstName = Lois; lastName = Lane; userid = "lois.lane"; } with mapping <RKEntityMapping:0x12d78e80 objectClass=User propertyMappings=( "<RKAttributeMapping: 0x12d819d0 userid => userid>", "<RKAttributeMapping: 0x12d85f70 lastName => lastName>", "<RKAttributeMapping: 0x12d72a10 firstName => firstName>", )> 2014-07-27 14:25:49.476 App[36854:3803] W restkit.core_data:RKManagedObjectMappingOperationDataSource.m:243 Performing managed object mapping with a nil managed object cache: Unable to update existing object instances by identification attributes. Duplicate objects may be created. 2014-07-27 14:25:49.477 App[36854:3803] D restkit.object_mapping:RKMapperOperation.m:231 Asked to map source object { firstName = Lois; lastName = Lane; userid = "lois.lane"; } with mapping <RKEntityMapping:0xd6c17a0 objectClass=User propertyMappings=( "<RKAttributeMapping: 0xd6b6ed0 userid => userid>", "<RKAttributeMapping: 0xd6c86b0 lastName => lastName>", "<RKAttributeMapping: 0xd6c1560 firstName => firstName>", )>]

Database:

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/qgZdV.png" data-original="https://i.stack.imgur.com/qgZdV.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

How can I avoid duplicates?

<strong>EDIT 1</strong>

- (RKManagedObjectStore *)setupCoreDataWithRESTKit { NSError * error; NSURL * modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"App" ofType:@"momd"]]; NSManagedObjectModel * managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy]; self.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel]; [self.managedObjectStore createPersistentStoreCoordinator]; NSArray * searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentPath = [searchPaths objectAtIndex:0]; NSPersistentStore * persistentStore = [self.managedObjectStore addSQLitePersistentStoreAtPath:[NSString stringWithFormat:@"%@/App.sqlite", documentPath] fromSeedDatabaseAtPath:nil withConfiguration:nil options:[self optionsForSqliteStore] error:&error]; NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error); if(!persistentStore){ NSLog(@"Failed to add persistent store: %@", error); } [self.managedObjectStore createManagedObjectContexts]; return self.managedObjectStore; }

Answer1:

You need to create a cache. This is how RestKit searches for and finds existing objects, which is the mechanism for avoiding duplicates. There are 2 types or cache, but you usually want to use the memory cache (as opposed to the fetch cache) as it is faster.

Create and set the cache with:

self.managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectSyore.persistentStoreManagedObjectContext];

Recommend

  • How to give each JButton in a 10 x 10 grid button layout a unique ID/name
  • R heatmap.2 One row heatmap with color key
  • How do I change datatype of an imported spreadsheet in mysql?
  • mov al, byte[si + bx] becomes mov al, [bx+si+1]
  • Java: compile-time resolution and “most specific method” as it applies to variable arity
  • Dropdown in AngularJS has Correct Text, but Wrong Value
  • Convert Character to binary assembly language
  • pdf parsing problem
  • How to show or hide element based on an Array of Geo ip
  • Problems with 'pyenchant', CentOS (& El Capitan)
  • Why does C++ require breaks in switch statements? [duplicate]
  • Collections: ConcurrentModificationException
  • How to make mobile devices not load an external JS
  • how combine multiple query (selected statment) in my case?
  • opencv structured forest edge detection and findcontours
  • UNION NULLS into View that can be queried later
  • Servlet error HTTP Status 405 - HTTP method GET is not supported by this URL
  • How to read the Intel Opcode notation
  • Sockets client reads the same over and over
  • How can I setting datasource in Spring application
  • freegeoip doesn't work anymore
  • How to restore default homepage?
  • Postgresql COPY empty string as NULL not work
  • creating road network datasets in R
  • Print results of a SELECT query as preformatted text in PHP?
  • ractivejs component nesting
  • How can my Java program be used to open a file when the file is double clicked? [closed]
  • Issue while saving the dynamic field values in the preferences
  • Count the occurrences of a specific value and remove them at the same time
  • concat columns by joining multiple DataFrames
  • Google Text To Speech as a sperate class that can be called when ever needed?
  • How to convert this 'for' loop to a vector solution
  • Loop to check lowercase letter in assembly
  • TASM Print characters of string
  • Dependency Injection - Proper use of interfaces? [closed]
  • PyInstaller Tkinter window low resolution in App bundle, but not in app program
  • Assembly INT 13h - read disk problem
  • Cannot connect to my database from within SQL Management Studio
  • Help me understand this C code (*(void(*) ()) scode) ()
  • ArrayList capacity size increasing strange behaviour