CoreData: Private context with child Main context - FetchedResultsController not getting updates

狂风中的少年 提交于 2020-01-01 19:41:15

问题


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 created Item back.

  • If I listen for NSManagedObjectContextObjectsDidChangeNotification notifications, they're indeed triggered after inserting.

  • When I relaunch the app, my inserted Items 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!