NSFetchedResultsController 与 table 类似 iMessage
NSFetchedResultsController with table like iMessage
我有核心数据数据库。结构在图像上(DBContact 有许多 属性 messages
的 DBMessages,每个 DBMessage 有一个 属性 contact
的联系人:
我想显示按联系人分组并按日期排序的邮件。所以行数必须=消息联系人的数量。在每一行中,我必须显示头像、联系人姓名和姓氏以及该联系人最后一条消息的文本和日期。
我找到的唯一解决方案是:
let entityName = kDBOrder
let entityDescription = NSEntityDescription.entityForName(entityName, inManagedObjectContext: managedObjectContext)
let fetchRequest = NSFetchRequest()
fetchRequest.entity = entityDescription
fetchRequest.fetchBatchSize = 20
// sort
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "contact.id", ascending: false), NSSortDescriptor(key: "date", ascending: false)]
let sectionNameKeyPath: String? = "contact.id"
self.fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: sectionNameKeyPath, cacheName: nil)
fetchedResultsController!.delegate = self
if tableViewMain != nil {
do {
try fetchedResultsController!.performFetch()
} catch {
print("An error occurred in fetchresultcontroller")
}
tableViewMain.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var count = 0
if let sections = fetchedResultsController?.sections {
let currentSection = sections[section]
count = currentSection.numberOfObjects
// in each section we must have only 1 row
if count > 0 {
count = 1
}
}
return count
}
但在这种情况下,联系人将按 ID 排序,但我需要按最后一条消息的日期对它们进行排序。但是,如果我将 sortDescriptors 更改为 fetchRequest.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false), NSSortDescriptor(key: "contact.id", ascending: false)]
以首先按日期排序,它将无法正确显示联系人。
有人知道如何实现吗?
我可能会更改模型以使生活更轻松并更早地进行更多处理。基本上,如果在 2 个实体之间添加另一个一对一的关系,并且每次添加新消息时都会更新该关系 - 因此您始终有一个指向最新消息的直接指针。
完成后,您的 FRC 和 table 加载速度就变得微不足道了,因为您只需在获取中使用联系人。新消息也得到正确处理,因为关系得到更新,因此联系人发生变化并且 FRC 将看到它。
我有核心数据数据库。结构在图像上(DBContact 有许多 属性 messages
的 DBMessages,每个 DBMessage 有一个 属性 contact
的联系人:
我想显示按联系人分组并按日期排序的邮件。所以行数必须=消息联系人的数量。在每一行中,我必须显示头像、联系人姓名和姓氏以及该联系人最后一条消息的文本和日期。
我找到的唯一解决方案是:
let entityName = kDBOrder
let entityDescription = NSEntityDescription.entityForName(entityName, inManagedObjectContext: managedObjectContext)
let fetchRequest = NSFetchRequest()
fetchRequest.entity = entityDescription
fetchRequest.fetchBatchSize = 20
// sort
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "contact.id", ascending: false), NSSortDescriptor(key: "date", ascending: false)]
let sectionNameKeyPath: String? = "contact.id"
self.fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: sectionNameKeyPath, cacheName: nil)
fetchedResultsController!.delegate = self
if tableViewMain != nil {
do {
try fetchedResultsController!.performFetch()
} catch {
print("An error occurred in fetchresultcontroller")
}
tableViewMain.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var count = 0
if let sections = fetchedResultsController?.sections {
let currentSection = sections[section]
count = currentSection.numberOfObjects
// in each section we must have only 1 row
if count > 0 {
count = 1
}
}
return count
}
但在这种情况下,联系人将按 ID 排序,但我需要按最后一条消息的日期对它们进行排序。但是,如果我将 sortDescriptors 更改为 fetchRequest.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false), NSSortDescriptor(key: "contact.id", ascending: false)]
以首先按日期排序,它将无法正确显示联系人。
有人知道如何实现吗?
我可能会更改模型以使生活更轻松并更早地进行更多处理。基本上,如果在 2 个实体之间添加另一个一对一的关系,并且每次添加新消息时都会更新该关系 - 因此您始终有一个指向最新消息的直接指针。
完成后,您的 FRC 和 table 加载速度就变得微不足道了,因为您只需在获取中使用联系人。新消息也得到正确处理,因为关系得到更新,因此联系人发生变化并且 FRC 将看到它。