paulw11 / Seam3

Cloudkit based persistent store for Core Data
Other
209 stars 25 forks source link

Fetched Results Controller Support for SMStore.predicate Method #68

Open Jerland2 opened 6 years ago

Jerland2 commented 6 years ago

Is there any chance someone could add support for Fetched Result Controllers and their delegates with the predicate method for NSPredicates? When using the predicate method my fetched result controllers fail to trigger its delegate methods. I have confirmed (at least I believe) that this issue is caused by the generated predicate due to the following SOF post: https://stackoverflow.com/questions/17151160/nsfetchedresultscontroller-delegate-not-called-for-api-listener

paulw11 commented 6 years ago

Can you provide a small example project that demonstrates the issue?

Jerland2 commented 6 years ago

@paulw11 as requested here is a sample project displaying the bug/problem. also I will describe steps to reproduce below: https://github.com/Jerland2/SeamPredicateTest

Steps to reproduce:

  1. Add 2 items using the "+" button on the initial page.
  2. Select an item
  3. Add Item on 2nd page -> NOTICE: It does not show up! This is the bug! The reason it does not show up is because the predicate is messing up the fetched results controller delegate. This predicate is the one generated from the SMStore .predicate method.
  4. In order to get items on 2nd page to show up: return to initial page and then navigate to the second page again.
Jerland2 commented 6 years ago

@paulw11 Wondering if there is any update/eta on this fix? I am really eager to add this feature to my app!!

paulw11 commented 6 years ago

I have been looking at this and I don't believe that it can be resolved. You can use the object context change notification as a trigger to reload your data and refresh your tableview. Something like:

NotificationCenter.default.addObserver(forName: NSNotification.Name.NSManagedObjectContextObjectsDidChange, object: CoreDataController.shared.viewContext, queue: nil) { (notification) in
    self.loadData()
 }

Add a self.tableView.reloadData() to your loadData function

Jerland2 commented 6 years ago

@paulw11 this is sad news, perhaps you could reference why you think it isn’t possible for someone to implement in the future here?

Your suggestion would work for my reloading, however it still does not support the entire FRC delegate pattern, which is a HUGE issue as far as supporting Core Data and apps that use FRC’s to perform operations.

paulw11 commented 6 years ago

The problem seems to be with the way the FRC monitors changes and the disconnect between CloudKit references and the local MOC references. I have found a different predicate that you can use in your sample that works with the FRC delegate:

 fetchRequest.predicate = NSPredicate(format:"booksToPerson.name = %@", selectedPerson.name!)
jwarrent commented 6 years ago

This predicate pattern worked for me. In my case, I generate a unique UUID key for my Person entity so I can compare something much more likely to stay unique and keep on using FRC.