简体   繁体   中英

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

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 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..).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM