我正在使用NSFetchedResultsController从数据库中获取数据并在UITableView中显示(元素根据时间戳排序)。现在我想使用fetchLimit属性仅加载最后20个项目,但fetchLimit
返回前20个对象而不是最后一个。
以下是我使用的代码:
lazy var messagesFetchController:NSFetchedResultsController<Message>? = {
let messagesFetchRequest = NSFetchRequest<Message>(entityName: "Message")
let sortDescriptor = NSSortDescriptor(key: Constants.kTimeStampKey, ascending: true)
messagesFetchRequest.sortDescriptors = [sortDescriptor]
messagesFetchRequest.fetchLimit = 5
let frc = NSFetchedResultsController(fetchRequest: messagesFetchRequest, managedObjectContext: CoreDataContentManager.managedObjectContext(), sectionNameKeyPath: "dateStamp", cacheName: nil)
frc.delegate = self
frc.fetchRequest.shouldRefreshRefetchedObjects = true
do {
try frc.performFetch()
}
catch {
print("Unable to fetch cart Objects")
}
return frc
}()
答案 0 :(得分:0)
您还需要使用.fetchOffset
一般过程:
.fetchLimit = 5
.fetchOffset = totalRecords - 5