Problem with performing add / update in CoreData

I have an NSArray there are several NSDictionary , every time I save an NSDictionary object and add it to NSArray , I send it to add / update function. I know that I read the data correctly, and when I used the data without the update function, all the data is inserted in CoreData , but I want to add the update function to check the data that I put. Right now, the function adds the first object, then go on and on to "update" again and again. What's wrong?

My code is:

 -(void)updateOrAddTrap:(NSMutableDictionary*)trapObject { NSInteger trapID = [trapObject[@"id"] integerValue]; Trap *trapEntity = nil; NSError *error = nil; Boolean success = true; self.fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Trap"]; self.predicate = [NSPredicate predicateWithFormat:@"trapID == %d", trapID]; [self.fetchRequest setPredicate:self.predicate]; NSArray *results = [self.managedObjectContext executeFetchRequest:self.fetchRequest error:&error]; if (results == nil) { // Handle error NSLog(@"HANDLE ERROR"); success = false; } else if ([results count] == 0) { // Nothing to update, add new trap NSLog(@"NOTHING TO UPDATE | ADD NEW"); // Create a new record (row) trapEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Trap" inManagedObjectContext:self.managedObjectContext]; } else { NSLog(@"UPDATE EXISTING OBJECT"); trapEntity = results[0]; // There should be only one object for the ID. } if (trapEntity != nil) { // Set properties for new or existing object ... // Int int alarmDistance = [[trapObject objectForKey:ALARMDISTANCE] intValue]; int type = [[trapObject objectForKey:TYPE] intValue]; int roadNumber = [[trapObject objectForKey:ROADNUMBER] intValue]; int trap_id = [[trapObject objectForKey:ID] intValue]; int polys = [[trapObject objectForKey:POLYGONS] intValue]; int degrees = [[trapObject objectForKey:DEGREES] intValue]; [trapEntity setAlarmDistance:[NSNumber numberWithInt:alarmDistance]]; [trapEntity setType:[NSNumber numberWithInt:type]]; [trapEntity setRoadNumber:[NSNumber numberWithInt:roadNumber]]; [trapEntity setTrapID:[NSNumber numberWithInt:trap_id]]; [trapEntity setPolys:[NSNumber numberWithInt:polys]]; [trapEntity setDegrees:[NSNumber numberWithInt:degrees]]; // Boolean BOOL isActive = [[trapObject objectForKey:ISACTIVE] integerValue] == 1 ? YES : NO; [trapEntity setIsActive:[NSNumber numberWithBool:isActive]]; // Float float lat = [[trapObject objectForKey:LAT] floatValue]; float lon = [[trapObject objectForKey:LON] floatValue]; [trapEntity setLat:[NSNumber numberWithFloat:lat]]; [trapEntity setLon:[NSNumber numberWithFloat:lon]]; // NSString [trapEntity setDirection:[trapObject objectForKey:DIRECTION]]; [trapEntity setTrapDescription:[trapObject objectForKey:DESCRIPTION]]; [trapEntity setPoly0:[trapObject objectForKey:POLYGON_A]]; [trapEntity setPoly1:[trapObject objectForKey:POLYGON_B]]; [trapEntity setPoly2:[trapObject objectForKey:POLYGON_C]]; [trapEntity setPolygonAzimut1:[trapObject objectForKey:POLYGON_A_AZIMUTH]]; [trapEntity setPolygonAzimut2:[trapObject objectForKey:POLYGON_B_AZIMUTH]]; [trapEntity setPolygonAzimut3:[trapObject objectForKey:POLYGON_C_AZIMUTH]]; // etc. for all properties ... if (![self.managedObjectContext save:&error]) { NSLog(@"HANDLE ERROR WHEN SAVING THE OBJECT"); success = false; } } } 

I will be happy to know how I can update correctly, as I also miss this feature right now.

EDIT: Thanks to martin, the code works like a charm.

+4
source share
1 answer

I am not sure if this is the cause of your problem, but you are checking for a condition error or for an empty result incorrect. It should be

 BOOL success = true; Trap *trap = nil; NSArray *results = [self.managedObjectContext executeFetchRequest:self.fetchRequest error:&error]; if (results == nil) { NSLog(@"HANDLE ERROR"); success = false; } else if ([results count] == 0) { NSLog(@"NOTHING TO UPDATE | ADD NEW"); trap = [NSEntityDescription insertNewObjectForEntityForName:@"Trap" inManagedObjectContext:self.managedObjectContext]; } else { NSLog(@"UPDATE EXISTING OBJECT"); trap = result[0]; // There should be only one object for the ID. } if (trap != nil) { // Set properties for new or existing object ... [trap setTrapID:[NSNumber numberWithInt:trap_id]]; // etc. for all properties ... if (![self.managedObjectContext save:&error]) { NSLog(@"HANDLE ERROR WHEN SAVING THE OBJECT"); success = false; } } 

executeFetchRequest returns

  • nil if an error occurred, and
  • empty array if no matching objects are found.
+4
source

Source: https://habr.com/ru/post/1489969/


All Articles