iOSDev
iOSDev

Reputation: 412

Application getting crashed in coredata for object ID for it's primary key field

I am using Coredata for the 1st time, I am setting some values to the userinfo entity, fetching some values from usergroups entity.

While doing this process my application is getting crashed saying:

'Item not previously assigned an object ID for it's primary key field, which is used to obtain a permanent ID for the Core Data object. Before a call to save on the managedObjectContext, be sure to assign an object ID.

This looks something like

[newManagedObject setValue:[newManagedObject assignObjectId] forKey:[newManagedObject
 primaryKeyField]]

. The item in question is:

 <Userinfo: 0x10695b30>  (entity: Userinfo; id: 0x10695b70  <x-coredata://18875EDC-D300-4D63-90FC-181B2487599D-3229-000024DF842CC304/Userinfo/tA6216695-816F-4379-B24C-984AAFD1A8342>; data:

// For Reading Values & modifying

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Userinfo" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSPredicate *datapredicate = [NSPredicate predicateWithFormat:@"username == %@",usermobileNumber];
    [fetchRequest setPredicate:datapredicate];

    [context executeFetchRequest:fetchRequest onSuccess:^(NSArray *results){
        NSLog(@"results %@",[results valueForKey:@"firstname"]);
        userinfoObject = [results objectAtIndex:0];
        NSLog(@"userinfo %@",userinfoObject);
//        userinfoObject = [[Userinfo alloc]initWithEntity:[NSEntityDescription entityForName:@"Userinfo" inManagedObjectContext:context] insertIntoManagedObjectContext:context];

        [userinfoObject setValue:[userinfoObject valueForKey:@"firstname"] forKey:@"firstname"];
        [userinfoObject setValue:[userinfoObject valueForKey:@"lastname"] forKey:@"lastname"];
        [userinfoObject setValue:[userinfoObject valueForKey:@"email"] forKey:@"email"];
        [userinfoObject setValue:[userinfoObject valueForKey:@"zipcode"] forKey:@"zipcode"];
        [userinfoObject setValue:[userinfoObject valueForKey:@"password"] forKey:@"password"];
        [userinfoObject setValue:[userinfoObject valueForKey:@"dateofbirth"] forKey:@"dateofbirth"];
        [userinfoObject setValue:[userinfoObject valueForKey:@"username"] forKey:@"username"];
        [userinfoObject setValue:[userinfoObject valueForKey:@"salutation"] forKey:@"salutation"];
//        [userinfoObject setValue:[userinfoObject assignObjectId] forKey:[userinfoObject primaryKeyField]];

// Adding Values

Usergroups *userGroupsObject = [NSEntityDescription insertNewObjectForEntityForName:@"Usergroups" inManagedObjectContext:context];
            [userGroupsObject setValue:contactObject.strGroupName forKey:@"groupname"];
            [userGroupsObject setValue:[contactsStack objectAtIndex:i] forKey:@"mobile_contacts"];
            [userGroupsObject setValue:usermobileNumber forKey:@"mobile_number"];
            [userGroupsObject setValue:[userGroupsObject assignObjectId] forKey:[userGroupsObject primaryKeyField]];
            NSError *error = nil;
            if (![context saveAndWait:&error]) {
                NSLog(@"There was an error! %@", error);
            }
            else {
                NSLog(@"You created Usergroup object!");
            }
            [userinfoObject addUsergroupsObject:userGroupsObject];

Upvotes: 0

Views: 798

Answers (2)

msv
msv

Reputation: 163

The assignObjectID and primaryKeyField methods are throwing off those who are trying to answer this question because they are StackMob specific methods. They don't directly interact with the Core Data ID at all, they just assign a randomly generated UUID to an attribute which maps to the primary key field in the database (StackMob servers). It is in fact mandatory to do this before calling save, as the incremental store implementation then uses that string ID to create a permanent Core Data ID in the overridden obtainPermanentIDsForObjects: method.

My guess here is that you have a managed object that before it's saved never gets assigned a value for the attribute which maps to the database primary key field. Before the save that leads to the crash, set a breakpoint and print out your managed object context's inserted objects ( i.e [self.managedObjectContext insertedObjects]). That should give you an idea of what object is causing the crash, and you can adjust your code accordingly.

Upvotes: 1

Alok Singh
Alok Singh

Reputation: 896

you should provide more code..........

generally adding new entry will look like this

FormField *info = [NSEntityDescription
                    insertNewObjectForEntityForName:@"FormField"
                    inManagedObjectContext:managedObjectContext];


[info setValue: @"string data to insert" forKey:@"fieldValue"];
NSError *error;
[managedObjectContext save:&error]

and retrieving an entry will look like this

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"entityName"
                                    inManagedObjectContext:managedObjectContext]];

NSError *error;
NSArray* data_ = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

Upvotes: 3

Related Questions