CoreData返回集带有空对象 [英] CoreData returning set with empty objects
问题描述
我在最后一天一直在研究这个问题,所以我认为是时候问这个问题了.
I've been looking into this issue for the last day, so I think it's time to ask the question.
我在CoreData中创建了一个一对多的自引用关系来存储联系人并将这些联系人分组.
I created a one-to-many self referencing relation in CoreData to store contacts and group these contacts.
没有父母的联系人实际上拥有其孩子的部门名称".
Contacts that haven't got a parent actually hold the "section name" of its children.
Section name (object without parent)
-------------------------------------
Contact 1
Contact 2
保存对象的代码(简体):
Code to save the objects (simplified):
//Create Section
section = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:_managedObjectContext];
[section setValue:self.sectionField.text forKey:@"name"];
[section setValue:nil forKey:@"parent"];
[_managedObjectContext save:&error];
//Create contact
NSManagedObject *newContact = [NSEntityDescription
insertNewObjectForEntityForName:@"Contact"
inManagedObjectContext:_managedObjectContext];
[newContact setValue:self.nameField.text forKey:@"name"];
[newContact setValue:self.numberField.text forKey:@"data"];
[newContact setValue:[NSString stringWithFormat:@"%@",[LoginController getUser][@"id"]] forKey:@"user_id"];
[newContact setValue:section forKey:@"parent"];
Tableviewcontroller:
在 viewWilAppear
中,我获取表视图的所有父"对象(= sections)并将其分配给 self.sections
属性.
Tableviewcontroller:
In viewWilAppear
I fetch all "parent"-objects (=sections) for the table view and assign them to the self.sections
property.
//In viewWillAppear I get all "parent" objects
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:_managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
// Set predicate
[request setRelationshipKeyPathsForPrefetching: [NSArray arrayWithObject:@"children"]];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(parent == nil) AND (children.@count > 0)"];
[request setPredicate:predicate];
self.sections = (NSMutableArray *)[_managedObjectContext executeFetchRequest:request error:nil];
然后在cellForRowAtIndexPath中,我尝试在索引位于indexPath.section的父级中检索索引位于indexPath.row的子级.
Then in cellForRowAtIndexPath I try to retrieve the child with index at indexPath.row within the parent with index at indexPath.section.
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
Contact * section = [self.sections objectAtIndex:indexPath.section];
NSArray * contacts = [section.children allObjects];
Contact * contact = [contacts objectAtIndex:indexPath.row];
NSLog(@"contact %@", contact.name);
[cell.textLabel setText:contact.name];
[cell.detailTextLabel setText:contact.data];
UIImageView *call = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Call"]];
[cell setAccessoryView:call];
return cell;
联系人的姓名不会显示,在这里我有点绝望.还有其他见解吗?
The name of the contact won't show and I'm getting kind of desperate here. Any other insights?
推荐答案
该实体的NSManagedObject类不正确.我不知道为核心数据实体自动生成这些对象的工具.
The NSManagedObject class for the entity was not correct. I wasn't aware of the tool to automatically generate these objects for core data entities.
新文件.>核心数据> NSManagedObject子类>选择正确的数据模型>选择正确的实体.
New file.. > Core Data > NSManagedObject subclass > Choose the correct Datamodel > Choose the correct entity.
这篇关于CoreData返回集带有空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!