0

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];
iOSDev
  • 412
  • 10
  • 30
  • What does the `primaryKeyField` method do? This is not a standard Core Data method, and it seems to be the cause of your trouble. Where did this come from? – Tom Harrington May 27 '13 at 20:40
  • @TomHarrington : This is in related to a database where i am using, and to that particular Nsmanaged Object context i.e the usergroups object i am assigning the objectId to the default primarykey that the database has created. – iOSDev May 28 '13 at 00:22
  • OK, I don't really understand what you're saying, but if to are mucking around with an NSManagedObject's objectID, you are pretty much guaranteed to get crashes and/or data corruption. – Tom Harrington May 28 '13 at 02:58

2 Answers2

3

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];
geo
  • 1,781
  • 1
  • 18
  • 30
Alok Singh
  • 896
  • 6
  • 18
  • 2
    The reason is you are trying to use object id of core data object which is just created but not saved yet... as soon as you call save on managed object context that object will gets its id. – Alok Singh May 27 '13 at 13:33
  • either call save on manage object context before that line or change the logic............ – Alok Singh May 27 '13 at 13:36
1

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.

msv
  • 163
  • 3
  • Yeah, The user object is having null values, msv do you have idea on stackmob ?? – iOSDev May 29 '13 at 02:27
  • I need to send a usergroup object to for a particular User object with username being the primary key, how can i achieve this.. – iOSDev May 29 '13 at 04:48