sectionNameKeyPath与NSFetchedResultsController无法正常工作

Rit*_*its 9 iphone cocoa-touch core-data objective-c nsfetchedresultscontroller

我有一个Order带有属性的实体paid,它是一个布尔值.

我想在a中显示所有订单UITableView,但我想将它们分为两部分:'未付款'和'付费'.所以我认为我只是给予"付费" sectionNameKeyPath,就像这样:

fetchedResultsController = [[NSFetchedResultsController alloc]
         initWithFetchRequest:fetchRequest
         managedObjectContext:managedObjectContext
           sectionNameKeyPath:@"paid"
                    cacheName:nil];
Run Code Online (Sandbox Code Playgroud)

根据我的推理,这将导致两个部分,其中第一部分包含所有带有paid = NO(0)的订单,第二部分包含paid = YES(1).

但是当我添加一个带有paid = YES的新订单时,它会显示在第一部分中.当我签入获取结果控制器委托时,我可以看到创建了一个带有indexPath [0,0]的新记录!为什么不插入第二部分?

tim*_*man 16

尝试将一个排序描述符数组添加到与NSFetchedResultsController一起使用的NSFetchRequest中.

您将首先要对付费布尔值进行排序,然后对要排序的其他任何内容进行排序.

Swift 3示例:

fetchRequest = ... // <- instantiate your fetch request

let sortByPaid = NSSortDescriptor(key: "paid", ascending: true)
let sortByCustomerName = NSSortDescriptor(key: "customer.name", ascending: true) // <- if Order had a relationship to Customer that had an attribute name 
fetchRequest.sortDescriptors = [sortByPaid, sortByCustomerName]

// now instantiate fetchedResultsController as in the question above
Run Code Online (Sandbox Code Playgroud)