Search code examples
iosobjective-ccore-datansmanagedobjectnsmanagedobjectcontext

NSManagedObject save only the latest object and not the whole objects


I've a json response I'm trying to save to Core Data using NSManagedObject with the following code:

- (void)breakTrapsToSave:(NSDictionary*)trapsDict
{    
    tempA = [trapsDict objectForKey:@"Envelope"];
    tempB = [tempA objectForKey:@"Body"];
    tempC = [tempB objectForKey:@"getTrapsResponse"];
    tempD = [tempC objectForKey:@"getTrapsResult"];
    tempE = [tempD objectForKey:@"TRAPS"];

    self.lastUpdate = [tempE objectForKey:@"lastUpdate"];
    [self.sharedPrefs setObject:self.lastUpdate forKey:@"last_update"];
    [self.sharedPrefs synchronize];
    NSLog(@"Traps latest updated at: %@", self.lastUpdate);

    tempF = [tempE objectForKey:@"TRAP"];

    [tempF enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        tempA = [tempF objectAtIndex:idx];
        NSString *finalResult;
        NSString *key;

        for (int i=0; i < node.count; i++) {
            tempB = [tempA objectForKey:[node objectAtIndex:i]];
            finalResult = (NSString*)tempB;
            key = [node objectAtIndex:i];

            [self addToCoreData_value:finalResult key:key]; // Core Data
        }
        counter = idx;
    }];

    NSLog(@"Total Traps: %d", counter);
}

#pragma mark - Core Data Methods

- (void)initCoreData
{
    IDANAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    context = appDelegate.managedObjectContext;
    entityDesc = [NSEntityDescription entityForName:@"Trap" inManagedObjectContext:context];
    managedObject = [[NSManagedObject alloc] initWithEntity:entityDesc insertIntoManagedObjectContext:context];
}

- (void)addToCoreData_value:(NSString*)value key:(NSString*)key
{
    if ([key isEqualToString:@"alarmDistance"] ||
        [key isEqualToString:@"degrees"] ||
        [key isEqualToString:@"id"] ||
        [key isEqualToString:@"polys"] ||
        [key isEqualToString:@"roadNumber"] ||
        [key isEqualToString:@"type"]) {

        [managedObject setValue:[NSNumber numberWithInt:[value intValue]] forKey:key];
    }
    else if ([key isEqualToString:@"lat"] ||
               [key isEqualToString:@"lon"]) {
        [managedObject setValue:[NSNumber numberWithFloat:[value floatValue]] forKey:key];
    }
    else  if ([key isEqualToString:@"isActive"]) {
        BOOL toSet = false;

        if ([value isEqualToString:@"True"]) {
            toSet = YES;
        }
        else {
            toSet = NO;
        }

        [managedObject setValue:[NSNumber numberWithBool:toSet] forKey:key];
    }
    else {
        [managedObject setValue:value forKey:key];
    }


    NSError *error;
    [context save:&error];
}

The thing is, I did log and I saw I get the correct values and keys thru the entire enumerateObjectsUsingBlock loop. Also, in:

 - (void)addToCoreData_value:(NSString*)value key:(NSString*)key

I did a log and I saw I get all the keys and values as well. But when I open the sqlite I see only one row in the table, the last object. NSLog(@"Total Traps: %d", counter); - also print the correct amount of objects (116 as for now).

What I'm doing wrong?


Solution

  • It looks like you only have one managed object. All you are doing is successively changing the properties of it.

    What you should be doing is creating a new managed object for each item you are saving.