Qeychon
Qeychon

Reputation: 491

NSManagedObject is automatically set to fault

I have the following classes:

class Profile {
  var state:State
  var member:Member?
}

class Member: NSManagedObject {
   @NSManaged var nickName: String?
   @NSManaged var mId: String?
}

The problem is located in the following class:

class RegisterMultiTableVContr: UITableViewController {

     var profile:Profile?

     override func viewDidLoad() {
         self.profileService.createEmptyProfile(
                 { (profile:Profile) in
                     self.profile = profile
                     println("\(self.profile!.member!.description)")
                     /*
                     Correct:
                     Output:
                     <meetpoll.Member: 0x7fd654175c10> (entity: Member; id: 0xd000000000c80000<x-coredata://21228B23-63B3-4708-8EAC-3302072BA074/Member/p5> ; data: {
                         mId = nil;
                         nickName = nil;
                     })
                     */
                 }, failure: { (error:String) in
                     // Exception Handling
             })
     }

     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
         println("\(self.profile!.member!.description)")
         /*
         Set to fault automatically
         Output:
         <meetpoll.Member: 0x7fd654175c10> (entity: Member; id: 0xd000000000c80000<x-coredata://21228B23-63B3-4708-8EAC-3302072BA074/Member/p50> ; 
         data: <fault>)
         */
     }
}

The prepareForSegue function is called only after the viewDidLoad. I wrote the output of the println function into the source code. My question is, why the object member is set to fault automatically? and how can i set the member fault to false again?

Include 2014-10-08

The function create an empty profile with Member object like this:

var member = NSEntityDescription.insertNewObjectForEntityForName("Member", inManagedObjectContext: context) as Member
var profile = Profile(member:member)
var error: NSError? = nil
if context.save(&error) {
   return profile
}

Include 2014-10-09

Ok i see you need more information, so I wrote a mini simple example and loaded up to GitHub https://github.com/qeychon/CoreDataBug/tree/master

Upvotes: 0

Views: 741

Answers (3)

Qeychon
Qeychon

Reputation: 491

The reason why the first object could be changed: the Core Data functions are performed asynchronously. Therefore, the object could be changed temporarily. After a certain time the method save is called (thank you @quellish) and suddenly the object has been set to read only (fault). The solution: the save method must be called later!

Upvotes: 0

Adi Pop
Adi Pop

Reputation: 226

You could set

[request setReturnsObjectsAsFaults:NO]

Update

Sorry for the delay I was answering from iPhone app

As quellish say there is nothing wrong getting faults when fetching from CoreData. Each of the objects will be 'fault' until you need to access their persistent values and you can force core data to retrieve the full object with the persistent values by setting

[request setReturnsObjectsAsFaults:NO]

Upvotes: 1

Terminus
Terminus

Reputation: 947

I do not know why it is set to fault automatically, it seems like something else is being called in-between them. You can easily turn your object into fault or full object with

  • [NSManagedObjectContext refreshObject:o mergeChanges: YES/NO ]

Where YES goes for an object and NO goes for a fault.

Upvotes: 0

Related Questions