0

Coredata 第一次使用,我正在为实体设置一些值,从userinfo实体中获取一些值usergroups

在执行此过程时,我的应用程序崩溃说:

'以前没有为其主键字段分配对象 ID 的项目,用于获取核心数据对象的永久 ID。在调用保存 managedObjectContext 之前,一定要分配一个对象 ID。

这看起来像

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

. 有问题的项目是:

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

// 用于读取值和修改

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]];

// 添加值

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];
4

2 回答 2

3

你应该提供更多的代码............

通常添加新条目将如下所示

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


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

检索条目将如下所示

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

NSError *error;
NSArray* data_ = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
于 2013-05-27T13:05:55.003 回答
1

assignObjectID 和 primaryKeyField 方法让那些试图回答这个问题的人望而却步,因为它们是 StackMob 特定的方法。它们根本不直接与 Core Data ID 交互,它们只是将随机生成的 UUID 分配给映射到数据库(StackMob 服务器)中的主键字段的属性。实际上,在调用 save 之前必须执行此操作,因为增量存储实现然后使用该字符串 ID 在覆盖的获取 PermanentIDsForObjects: 方法中创建永久核心数据 ID。

我在这里的猜测是,您有一个托管对象,在保存它之前从未为映射到数据库主键字段的属性分配值。在导致崩溃的保存之前,设置断点并打印出托管对象上下文的插入对象(即 [self.managedObjectContext insertedObjects])。这应该让您了解导致崩溃的对象,并且您可以相应地调整您的代码。

于 2013-05-28T17:28:21.963 回答