我第一次使用Coredata
,我正在为userinfo
实体设置一些值,从usergroups
实体中获取一些值。
在执行此过程时,我的应用程序崩溃了:
'项目之前没有为其主键分配对象ID field,用于获取Core Data的永久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:
//阅读价值观&amp;修改
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];
答案 0 :(得分: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];
答案 1 :(得分:1)
assignObjectID和primaryKeyField方法正在抛弃那些试图回答这个问题的人,因为他们是StackMob特定的方法。它们根本不直接与Core Data ID交互,它们只是将一个随机生成的UUID分配给一个属性,该属性映射到数据库中的主键字段(StackMob服务器)。事实上,在调用save之前必须这样做,因为增量存储实现然后使用该字符串ID在重写的obtainPermanentIDsForObjects:方法中创建永久的Core Data ID。
我的猜测是,您有一个托管对象,在保存之前,永远不会为该属性分配一个映射到数据库主键字段的值。在导致崩溃的保存之前,设置断点并打印出托管对象上下文的插入对象(即[self.managedObjectContext insertedObjects])。这应该可以让您了解导致崩溃的对象,并且可以相应地调整代码。