Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Core data 具有零值的核心数据获取和排序_Core Data_Nsfetchedresultscontroller - Fatal编程技术网

Core data 具有零值的核心数据获取和排序

Core data 具有零值的核心数据获取和排序,core-data,nsfetchedresultscontroller,Core Data,Nsfetchedresultscontroller,我在使用核心数据时遇到了一个问题 在我的核心数据模型中,我有Grove实体和Tree实体,在Grove和Tree之间有一对多的关系,在Tree和Grove之间有一对一的关系 (每棵树最多可在一个小树林中) 但是一些树可能是“野生的”,不属于任何小树林 我正在尝试使用fetResultController在表视图中显示我的所有树。 每个小树林都是表视图的一部分,“野生”树必须位于单独的部分 我的代码如下: NSEntityDescription *entity = [NSEntityDescrip

我在使用核心数据时遇到了一个问题

在我的核心数据模型中,我有
Grove
实体和
Tree
实体,在
Grove
Tree
之间有一对多的关系,在
Tree
Grove
之间有一对一的关系 (每棵树最多可在一个小树林中)

但是一些
可能是“野生的”,不属于任何
小树林

我正在尝试使用fetResultController在表视图中显示我的所有树。 每个小树林都是表视图的一部分,“野生”树必须位于单独的部分

我的代码如下:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tree" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sectionSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Grove.name" ascending:YES];
    NSSortDescriptor *rowSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];
    NSArray *sortDescriptors = @[sectionSortDescriptor, rowSortDescriptor];
    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:@"Grove.name" cacheName:nil];
正如你所看到的,我正在取回所有的树。 用于确定节的第一个排序描述符基于树与树丛之间的一对一关系,并查找树丛名称。 第二个排序描述符用于对给定节内的树进行排序,并按类型对它们进行排序

当树定义了一个树丛时,这段代码工作得非常好。 但是grove=nil的树不能通过此请求获取。 你知道一种实现想要的行为的方法吗?
我必须为野生树木创建一个假的树丛吗?

我建议按照您的建议,创建一个表示“无”的特殊的
树丛
实例

另一个解决方案是获取groves并操作表视图数据源方法,如下所示:

  • 返回结果数+1作为节数。最后一段是野生树木
  • 根据返回的grove实体配置节头
  • 返回grove.trees.count作为节中的行数,对最后一节中的其他树进行特殊提取
  • 等等

你明白要点了。使用fetched results controller有点复杂,而且是一种非标准的用法,但我以前认为它工作得很好。

我选择了简单的“无”选项解决方案