Sorting sections issue in NSFetchedResultsController
I'm doing a chat app, where I'm storing chat time for each chat, now I need to display latest chats first, grouped in section, for eg: If I have 10 chats of today it should be under section named July 10, 2016 with last send chat at last. For this I'm sorting the list by chatTimestamp and I have another sectionDate field which stores the corresponding date. Below is a sample format of data that displays in core data.
<MyChat.ChatData: 0x7f8b71cdd190> (entity: ChatData; id: 0xd000000000140002 <x-coredata:….> ; data: {
chatDeviceType = ipad;
chatId = 3557;
chatOwnerName = “John Mathew”;
chatReadStatus = 1;
chatStatus = Received;
chatText = “Hi how are you?“;
chatTimestamp = "2015-09-21 10:41:37 +0000";
chatType = Mine;
imageData = nil;
imageUrl = nil;
sectionDate = "Sep 21, 2015";
users = "0xd000000000080000 <x-coredata:…>”;
})
This is a portion of my code so far
var fetchedResultsController: NSFetchedResultsController = NSFetchedResultsController()
override func viewDidLoad() {
super.viewDidLoad()
setupFRC(limit: LIMIT)
......
}
func setupFRC(limit limit:Int) {
messageMaxLimit += limit
let objectsCount = self.stack.mainContext.countForFetchRequest(fetchRequest(self.stack.mainContext), error: nil)
NSFetchedResultsController.deleteCacheWithName("Root")
let request = self.fetchRequest(self.stack.mainContext)
self.fetchedResultsController = NSFetchedResultsController(fetchRequest: request,
managedObjectContext: self.stack.mainContext,
sectionNameKeyPath: "sectionDate",
cacheName: "Root")
self.fetchedResultsController.delegate = self
self.messageMaxLimit = self.messageMaxLimit > objectsCount ? objectsCount : self.messageMaxLimit
if objectsCount > self.messageMaxLimit
{
self.fetchedResultsController.fetchRequest.fetchOffset = objectsCount - self.messageMaxLimit
}
self.fetchData()
}
//To fetch data in FRC
func fetchData() {
do {
try self.fetchedResultsController.performFetch()
chatCollectionView.reloadData()
} catch {
assertionFailure("Failed to fetch: (error)")
}
}
func fetchRequest(context: NSManagedObjectContext) -> FetchRequest<ChatData> {
let e = entity(name: "ChatData", context: context)
let fetch = FetchRequest<ChatData>(entity: e)
fetch.predicate = NSPredicate(format: "(SELF.users == %@)", currentUser!)
//Sort by chatTimeStamp
let sortDescriptor = NSSortDescriptor(key: "chatTimestamp", ascending: true)
fetch.sortDescriptors = [sortDescriptor]
return fetch
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
do {
try self.fetchedResultsController.performFetch()
} catch {
assertionFailure("Failed to fetch: (error)")
}
chatCollectionView.reloadData()
}
Now the problem is that the chats seems to have order correctly, but the sections are in alphabetic order. But If I enter a chat and send it and when it reloads from controllerDidChangeContent it gets corrected. I can't figure out why it doesn't load in correct order initially.I'm using a collection view for this. Am I doing anything wrong here?
我通过删除sectionDate的NSManaged属性解决了这个问题,并像下面那样添加了它,并在核心数据模型中创建了属性瞬态并且工作。
var sectionDate: String {
get {
self.willAccessValueForKey("sectionDate")
var ddtmp = self.primitiveValueForKey("sectionDate") as! String?
self.didAccessValueForKey("sectionDate")
if (ddtmp == nil)
{
ddtmp = Utilities.stringFromDate(self.chatTimestamp!, dateFormat: "MMM dd, yyyy")
self.setPrimitiveValue(ddtmp, forKey: "sectionDate")
}
return ddtmp!
}
}
链接地址: http://www.djcxy.com/p/6150.html