通过段控制优化核心数据获取请求

发布于 2025-01-02 16:48:16 字数 264 浏览 3 评论 0原文

我有一个包含 5 个项目的分段控件,在选择每个项目时,数据会根据某些条件进行过滤,并显示不同的结果。段控制中的所有五个选择都使用相同的实体来获取数据。

目前我有一个 fetchresultcontroller ,每当段控件中的值发生变化时,我就会使用不同的谓词从同一实体中获取数据,并使用新数据重新加载表。

我正在寻求优化这一点。我的做法是否正确,或者什么是正确的做法?

另外,更改已获取的数据的升序和降序之间的排序顺序的最佳方法是什么。

感谢副词。

I have a segment control with 5 items, on selecting every item data is filter on some criteria and a different result is displayed. All the five choices in segment control use the same entity to fetch the data.

Currently i have a fetchresultcontroller and whenever there is a value change in the segment control i fetch data from the same entity with a different predicate and reload the table with new data.

I am looking to optimize this. Am I doing it the right way or what is the right way to do it?

Also what is the best way to change the sorting order between ascending and descending for an already fetched data.

Thanks in adv.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦言归人 2025-01-09 16:48:16

您可以发布一些代码片段吗?

如果您已经获取了一组 MangedObjcts,则可以使用排序描述符对 NSArray 重新排序: sortedArrayUsingDescriptors

- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors

Can you post some code snippets?

If you already fetched a set of MangedObjcts you can reorder the NSArray with a Sort Descirptor: sortedArrayUsingDescriptors

- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors
一片旧的回忆 2025-01-09 16:48:16

如果您只获取少量 NSManagedObject,则可能不需要进行太多优化。 CoreData 及其相应的类(例如您正在使用的 FetchResultsController,其设计与 UITableViews 配合得特别好)会为您完成大部分繁重的工作。

关于更改升序和降序排序顺序的最佳方式;这是通过如下方式确定的:

NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"someEntityProperty" ascending:YES/NO];

如果将升序设置为 YES,您将根据您提供的键将 NSManagedObjects 返回按最小到最大升序排序。如果将其设置为“否”,则会将它们恢复为最大到最小(降序)。

If you're only fetching a small number of NSManagedObjects, there probably isn't much optimization to be done. CoreData and its corresponding classes (such as the FetchResultsController you are using, which is designed to work particularly well with UITableViews) do most of the heavy lifting for you.

In terms of the best way to change the sorting order between ascending and descending; this is determined in something like this:

NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"someEntityProperty" ascending:YES/NO];

If you set ascending to YES, you'll get your NSManagedObjects back sorted smallest to largest ascending) on the key you provide. If you set it to NO, you'll get them back largest to smallest (descending).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文