【问题标题】:Predicates and combined filters in core data?核心数据中的谓词和组合过滤器?
【发布时间】:2015-06-25 13:59:15
【问题描述】:

我目前没有使用 Core Data,但我即将从 Realm 切换到它,如果我确定某件事会起作用,这对我的应用程序当前的工作方式至关重要。我有一个包含两个对象的模型:

Message:
date - NSDate
text - String
images - One-to-many > Image

Image:
imageURL - String
message - Many-to-one > Image

我有一个同时显示图像和消息的表格视图。当 UITableView 想知道时,我需要知道在某个索引路径上应该做什么。所以我需要能够执行类似于以下的查询:

“给我一个有序的图像和消息列表,消息按date排序,图像按message.date排序”。

我该怎么做?

【问题讨论】:

  • 你的tableView会被分组吗?如果是这样,部分名称是什么?

标签: ios swift uitableview core-data nspredicate


【解决方案1】:

据我所知,您无法在 coredata 获取请求期间对包含消息和图像的混合数组进行排序。这是一种可能的方法:

  1. 分别获取消息和图像
  2. 将两个数组合并为一个
  3. 排序唯一的一个数组- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context

功能

 NSInteger sort(id item1, id item2, void *context) {
        int v1 = [item1 iskindofClass[message class]]?item1.data:item1.message.date;
        int v2 = [item2 iskindofClass[message class]]?item1.data:item1.message.date;
        if (v1 < v2)
            return NSOrderedAscending;
        else if (v1 > v2)
            return NSOrderedDescending;
        else
            return NSOrderedSame;
    }

【讨论】:

  • 如果我有一个包含 10000 个项目的数组,那会不会很慢?
  • @Andrew 我不确定。最好进行一些测试以找出答案
猜你喜欢
  • 2017-09-12
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-21
相关资源
最近更新 更多