ios - CoreData 返回空对象集

标签 ios iphone core-data relationship

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

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

object

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

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

表格 View Controller :

viewWilAppear 中,我获取 TableView 的所有“父”对象(=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;

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

最佳答案

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

这一行:

NSArray *contacts = [section.children allObjects];

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

您可以更改要排序的关系,或者您可以对获得的数组进行排序。在调用 objectAtIndex: 将返回正常结果之前,您需要执行以下操作之一。

关于ios - CoreData 返回空对象集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22555166/

相关文章:

ios - 通过 TraceGL 分析 Objective C 中的代码路径?

ios - 为什么更改UILabel的文本不会在iOS 13上重置文本属性?

iphone - iOS 在 iOS 6 中隐藏标签栏会创建黑条(针对 iOS 6 的修复破坏了 iOS 7!)

ios - 核心数据 UML 图思维模式

iphone - 将数据库直接加载到 CoreData

ios - UIScrollView 内可选择的 UITableViewCells

iOS (Swift) 应用程序无法加载第 251 个声音资源

c# - Objective-C/iPhone 中的公钥加密

ios - 无法访问 tableview 属性以获取在自定义单元格中单击的按钮的索引

iphone - managedObjectModel 为 nil(无法使用 nil 模型创建 NSPersistentStoreCoordinator)