(Using swift / Xcode beta 3)
I have two entities in my project - a parent entity which has a one-to-many relationship with its children. When adding new objects before saving the context, everything works fine. However, after restarting the app and fetching parent object again I'm receiving 'relationship fault' for all of its children. This is how I'm saving my context :
func saveContext () {
var error: NSError? = nil
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context = appDel.managedObjectContext
if context == nil {
return
}
if !context.hasChanges {
return
}
if context.save(&error) {
return
}
println("Error saving context: \(error?.localizedDescription)\n\(error?.userInfo)")
abort()
}
I tried changing includesSubentities = true and setReturnsObjectsAsFaults = false but it doesn't seem to help. Most of the answers to 'relationship fault' problem with Objective-C seemed to use setRelationshipKeyPathsForPrefetching but using it with NSFetchRequest in Swift seems to be impossible.
Is there something I'm missing?
From the moment we ask for the value of a property of one of the records, Core Data jumps into action and fetches the data from the persistent store. This is better known as firing a Core Data fault.
Inverse relationships enable Core Data to propagate change in both directions when an instance of either the source or destination type changes. Every relationship must have an inverse. When creating relationships in the Graph editor, you add inverse relationships between entities in a single step.
Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.
Fetched Properties in Core Data are properties that return an array value from a predicate. A fetched property predicate is a Core Data query that evaluates to an array of results.
As GeneratorOfOne says, the fault just means the object hasn't be fetched into memory yet. And you are correct that you "cannot get those child objects to fetch and simply accessing them doesn't do the job." To cause the object to be fetched, you have to assess a property of the object, that is, actually use a value from the object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With