I am an extreme rookie with CoreData and I am attempting to section my tableView using fetchedResultsController using a custom function. My current implementation did not manage to section the tableView and I am still given just 1 section.
I refer to the following posts in my implementation: here and here. I also adopted the transient property.
I first create the NSManagedObject subclass using Xcode (Editor -> Create NSMangedObject Subclass) and added the var sectionIdentifier
to return a custom string. Unfortunately, my frc returns only 1 section.
// from the Conversation+CoreDataProperties.swift file automatically generated by Xcode
import Foundation
import CoreData
extension Conversation {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Conversation> {
return NSFetchRequest<Conversation>(entityName: "Conversation")
}
@NSManaged public var conversationStartTime: Double
@NSManaged public var profileImage: NSData?
@NSManaged public var recipientID: String?
@NSManaged public var recipientUsername: String?
@NSManaged public var shoutoutID: String?
@NSManaged public var unreadMessagesCount: Int32
var sectionIdentifier: String? {
let presentTimestamp = NSDate().timeIntervalSince1970
if conversationStartTime < presentTimestamp - Double(Constants.PermissibleDurationInMinutes * 60) {
return "Expired Conversations"
} else {
return "Active Conversations"
}
}
}
//at VC
lazy var fetchedResultsController: NSFetchedResultsController<Conversation> = {
let context = CoreDataManager.shared.persistentContainer.viewContext
let request: NSFetchRequest<Conversation> = Conversation.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "conversationStartTime", ascending: true)]
let frc = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: "sectionIdentifier", cacheName: nil)
frc.delegate = self
do {
try frc.performFetch()
} catch let err {
print(err)
}
return frc
}()
Printing out the conversation entity in console returns this
<Conversation: 0x604000e93100> (entity: Conversation; id: 0xd000000003e00000 <x-coredata://91BC90B2-9A0C-45A7-9B82-844BE88BAFE0/Conversation/p248> ; data: {
conversationStartTime = "1521359598.88445";
profileImage = <ffd8ffe0 00104a46 49460001 02000001 00010000 ffed009c 50686f74 6f73686f 7020332e 30003842 494d0404 00000000 0080>;
recipientID = "-L7rvH71i-KUXvLQVDOh";
recipientUsername = Angemon;
sectionIdentifier = nil;
shoutoutID = "-L7rvH71i-KUXvLQVDOh";
unreadMessagesCount = 0; })
Somehow sectionIdentifier is always nil. Any advice why is this happening? At the end of the day, I want to divide my list of conversations into two sections, first section "Active Conversations" and second section "Expired Conversations" depending how long ago that conversation is.
UPDATED CODE:
// At Conversation+CoreDataProperties.swift
import Foundation
import CoreData
extension Conversation {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Conversation> {
return NSFetchRequest<Conversation>(entityName: "Conversation")
}
@NSManaged public var conversationStartTime: Double
@NSManaged public var profileImage: NSData?
@NSManaged public var recipientID: String?
@NSManaged public var recipientUsername: String?
@NSManaged public var shoutoutID: String?
@NSManaged public var unreadMessagesCount: Int32
@objc var sectionIdentifier: String {
willAccessValue(forKey: "sectionIdentifier")
let presentTimestamp = NSDate().timeIntervalSince1970
var text = ""
if conversationStartTime < presentTimestamp - Double(Constants.PermissibleDurationInMinutes * 60) {
text = "Expired Conversations"
} else {
text = "Active Conversations"
}
didAccessValue(forKey: "sectionIdentifier")
return text
}
}
//At VC
lazy var fetchedResultsController: NSFetchedResultsController<Conversation> = {
let context = CoreDataManager.shared.persistentContainer.viewContext
let request: NSFetchRequest<Conversation> = Conversation.fetchRequest()
request.sortDescriptors = [
NSSortDescriptor(key: "conversationStartTime", ascending: false)
]
let frc = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: "sectionIdentifier", cacheName: nil)
frc.delegate = self
do {
try frc.performFetch()
} catch let err {
print(err)
}
return frc
}()