Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relationship fault with core data

(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?

like image 405
Paweł Avatar asked Jul 30 '14 09:07

Paweł


People also ask

What is data fault in Core Data?

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.

What is relationship in Core Data?

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.

What is the purpose of Core Data?

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.

What is a Core Data fetched property?

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.


1 Answers

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.

like image 178
Don Avatar answered Sep 20 '22 20:09

Don