【问题标题】:CoreData returning set with empty objectsCoreData 返回带有空对象的集合
【发布时间】:2014-04-28 14:35:54
【问题描述】:

我最近一天一直在研究这个问题,所以我认为是时候提出这个问题了。

我在 CoreData 中创建了一个一对多的自引用关系来存储联系人并对这些联系人进行分组。

没有父级的联系人实际上持有其子级的“部分名称”。

Section name (object without parent)
-------------------------------------
Contact 1
Contact 2

保存对象的代码(简化):

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

表格视图控制器:

viewWilAppear 中,我获取表视图的所有“父”对象(=sections)并将它们分配给self.sections 属性。

//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 的子级。

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;

联系人的姓名不会显示,我在这里有点绝望。还有其他见解吗?

【问题讨论】:

  • 只是对您的设计的建议。为联系人及其关系创建两个表。自我引用可能会导致内存问题。
  • 好的调用,将使用该实现作为良好实践。

标签: ios iphone core-data relationship


【解决方案1】:

实体的 NSManagedObject 类不正确。 我不知道为核心数据实体自动生成这些对象的工具。

新建文件.. > 核心数据 > NSManagedObject 子类 > 选择正确的数据模型 > 选择正确的实体。

【讨论】:

    【解决方案2】:

    除了你自己的答案,你还有一个问题:

    这一行:

    NSArray *contacts = [section.children allObjects];
    

    正在从集合中获取一个数组 (allObjects)。一个集合没有顺序,所以每次你请求这个数组时,它都会有不同的顺序。这意味着您的单元格没有确定性的内容,您将有重复的行和缺失的行。

    您可以更改要排序的关系,也可以对获得的数组进行排序。在调用 objectAtIndex: 之前,您需要执行其中一项操作才能返回正常结果。

    【讨论】:

    • 感谢您指出这一点,我感觉订购时发生了一些事情。像这样修复它。 NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" 升序:YES]; NSArray * 联系人 = [[section.contacts allObjects] sortedArrayUsingDescriptors:@[nameDescriptor]];
    猜你喜欢
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 2010-09-25
    • 2019-11-27
    相关资源
    最近更新 更多