Maybe the title of question is not apriopriate, but here is what I need to achieve with my NSFetchedResultsController
:
private func setupOnceFetchedResultsController() {
if fetchedResultsController == nil {
let context = NSManagedObjectContext.MR_defaultContext()
let fetchReguest = NSFetchRequest(entityName: "WLComment")
let createdAtDescriptor = NSSortDescriptor(key: "createdAt", ascending: true)
fetchReguest.predicate = NSPredicate(format: "item.identifier = %d", item.identifier)
fetchReguest.sortDescriptors = [createdAtDescriptor]
fetchReguest.fetchLimit = 10
fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchReguest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
try! fetchedResultsController.performFetch()
tableView.reloadData()
}
}
Suppose I have about 100 comments. The oldest on the top, and the latest on the bottom. There is no problem when I need to display all of them, but here I need only 10 of them. The above NSFetchedResultsController
will display the first 10 of 100 messages, but I need to display the latest 10.
Is there a way to let it know what I need?
Depending on how you are going to use the data later on, you can choose from at least two options:
revert the sort descriptor, will return the end of the sorted list with reverted order;
get the number of items first and set the appropriate fetchOffset
, will return the required number of items, ordered as needed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.