问题
I've been trying to build a Core Data stack as described by Marcus Zarra, where he has a Private Queue context and a Main Queue context (where the Main Queue context is a child of the Private Queue context).
I believe I've correctly rebuilt (here) his described MCPersistenceController
faithfully in Swift (the example code was Obj-C).
The problem is in my ListViewModel class (which contains an NSFetchedResultsController
). No matter what I try, its delegate callbacks (controllerDidChangeContent
etc) don't get called when a new Item
object is inserted.
The FRC and NSFetchRequest use the Main Context; the item is inserted on the main context; the save function save the Main Context and then merge the changes into the Private Queue
If I perform a manual
executeFetchRequest
on either context after inserting and saving, I do get the newly createdItem
back.If I listen for
NSManagedObjectContextObjectsDidChangeNotification
notifications, they're indeed triggered after inserting.When I relaunch the app, my inserted
Item
s are now shown.
I can only assume it's an issue with doing something on the wrong thread and there being no reported error, but no matter what I've tried, when I insert a new Item
the FRC triggers no delegate callback. Possibly it's some Swift thing that I've missed.
I'd really appreciate any suggestions at this point 😏.
My simple proof-of-concept project (Swift 1.2) is on GitHub. (I didn't get to the CloudKit stuff yet..).
回答1:
Your ListViewModel
object is a pure Swift object. The fetched results controller uses NSObject-descended functionality to check if the delegate responds to the delegate methods.
@objc
class ListViewModel: NSFetchedResultsControllerDelegate{
Fixes it.
来源:https://stackoverflow.com/questions/31722142/coredata-private-context-with-child-main-context-fetchedresultscontroller-not