Is it possible to access non-persistent variables through NSFetchedResultsController
?
I am having Contact entity created from core-data.
I added one more variable to it which I don’t want to persist, I want this variable only for the period of application live scope, then discard.
import CoreData
class Contact: NSManagedObject {
// Insert code here to add functionality to your managed object subclass
var incomingRequest = false <———————— temporary variable
override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?) {
super.init(entity: entity, insertIntoManagedObjectContext: context)
}
}
I am using NSFetchedResultsController
to show all the contacts on table view.
When I set incomingRequest, NSFetchedResultsController
doesn’t trigger didChangeObject
delegate, that I can understand, it’s obvious.
When I fetch contact from NSFetchedResultsController
and try to access incomingRequest
, it always gives me false.
Is there any workaround to get the previously set value of incomingRequest
by fetching NSFetchedResultsController
?
Or saving incomingRequest
too in core data is the only solution?
I know about transient
property in core data, but I want to use ivars, let's say in future non-persistent properties will get increased.
Thanks in advance :)
Any suggestions are welcome!
Is there any workaround to get the previously set value of incomingRequest by fetching
NSFetchedResultsController
?
No, because there isn't any previously set value. If you do the same fetch more than once, you're not guaranteed to get the same object instance. This is especially true if you're doing fetches from different managed object contexts, where you're guaranteed to not get the same object. They represent the same instance in the persistent store, but they're not the same object in memory.
So, you set incomingRequest
to true
on one object, but then you use your fetched results controller and get a different object. But you haven't set a value of incomingRequest
on this other object, so it gets the default value.
Whether to include incomingRequest
in your data model depends on what that value means and how you use it. That might be the right answer. You might prefer to do something like keep a set of NSManagedObjectID
s, as the incoming request collection, so you could always find out if a fetched object should be considered as having an incoming request.