在保存项目的同时保留有序关系的顺序

时间:2018-12-13 15:10:36

标签: ios swift core-data nsfetchrequest

我有以下(简化的)数据模型:

enter image description here enter image description here

以及以下提取请求:

static func fetchRequestForItems(fromListWithName name: String) -> FetchRequest?  {

let request = Item.sortedFetchRequest

    request?.predicate = NSPredicate(format: "ANY %K.%K like %@", #keyPath(Item.lists), #keyPath(ItemList.name), name)
    request?.sortDescriptors = [NSSortDescriptor(key: "items", ascending: true)]

    return request
}

称呼为:

let request = Item.fetchRequestForItems(fromListWithName: "Favorites")!
let result = try! persistentContainer.viewContext.fetch(request)

我要达到的目的是按照将Item添加到ItemList的顺序来获取它们

但是我当前遇到此错误:

caught "NSUnknownKeyException", "[<Item 0x600003b7ff20> valueForUndefinedKey:]: the entity Item is not key value coding-compliant for the key "items"."

有人知道如何在不出错的情况下获取正确排序的商品吗?

更新

有关为什么执行创建NSFetchRequest<Item>而不是NSFetchRequest<ItemList>的更多背景信息。原因是我希望使用类型为NSFetchedResultsController<Item>的获取的结果控制器,更重要的是它是委托方法func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?),该方法应该给我indexPath的顺序与...的.items相同。 ItemList

0 个答案:

没有答案