12

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

Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76
Paweł
  • 1,201
  • 1
  • 14
  • 27
  • Relationship fault is fine. In term of core data the fault is not the real object. It has not been yet realized, so when you try to access the properties, it goes and fetches from the persistent store and so the object builds up. – Sandeep Jul 30 '14 at 09:57
  • Hey, thanks for answer. Unfortunately I cannot get those child objects to fetch and simply accessing them doesn't do the job. – Paweł Jul 30 '14 at 10:04
  • Please show the code how you try to fetch the parent and its child objects. – zisoft Jul 30 '14 at 12:18
  • @Paweł did u get a solution for this ? – Mr.G Sep 02 '16 at 17:44

2 Answers2

3

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.

Don
  • 3,654
  • 1
  • 26
  • 47
0

This is tottaly normal . It gives you the fault issue unless you use the object , it want load in to memory until u use it

Mr.G
  • 1,275
  • 1
  • 18
  • 48