Ian Dundas
Ian Dundas

Reputation: 572

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.

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

Upvotes: 1

Views: 494

Answers (1)

jrturton
jrturton

Reputation: 119242

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.

Upvotes: 4

Related Questions