1 | initial version |
To ensure that iOS Core Data fetches an entity that has just undergone a relationship update, you can either perform a fetch request with sorting and filtering options, or you can use an NSFetchedResultsController.
let fetchRequest: NSFetchRequest<EntityA> = EntityA.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "relationshipToB == %@", specificBInstance)
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "attributeName", ascending: true)]
let fetchRequest: NSFetchRequest<EntityA> = EntityA.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "attributeName", ascending: true)]
let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
controller.delegate = self
do {
try controller.performFetch()
} catch let error {
print("Could not fetch: \(error.localizedDescription)")
}
By using either of these methods, you can ensure that you are always fetching the latest version of your data, including any changes to relationships.