gpt4 book ai didi

iOS 核心数据 : Refetch of subcategories necessary?

转载 作者:行者123 更新时间:2023-11-29 03:23:11 25 4
gpt4 key购买 nike

我目前正在优化我的应用程序。我的表结构简化如下:

主类别

  • 姓名
  • 位置
  • 有子类别

子类别

  • 姓名
  • 位置
  • 属于MainCategory

现在,如果用户在第一个 View Controller 中选择一个主要类别,他将转到下一个显示特定主要类别的所有子类别。到目前为止,代码如下:

- (void)setupFetchedResultsController
{
NSError *error = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"SubCategory"];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"position" ascending:YES]];
[self.mainCategory.managedObjectContext executeFetchRequest:request error:&error];
request.predicate = [NSPredicate predicateWithFormat:@"belongsToMainCategory = %@", self.mainCategory];

self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:request
managedObjectContext:self.mainCategory.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
}

现在我开始怀疑我是否需要这样做,因为如果我将我的数据模型设置为主要类别,我将在 mainCategory.hasSubcategories 中获得所有信息。然而这些对象是如何排序的呢?或者像我现在那样重新获取真的更容易吗?

最佳答案

我的简单建议是保留该代码。如果您访问 mainCategory.hasSubcategories,您将拥有一个未排序的 NSSet。所以,您需要自己订购。

在我的代码中,如果我需要像您那样做,我通常会使用这种模式。它非常简单,可以重复使用。此外,如果您担心表演,不用担心。引擎盖下的 Core Data 维护一个缓存,可让您提高检索性能。

如果你想加快速度,你还可以依赖 NSFetchRequest 类的 setRelationshipKeyPathsForPrefetching 方法。

关于iOS 核心数据 : Refetch of subcategories necessary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20815000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com