0

My data model looks like this:

Object A <----->> Object B <-----> Object C

I fetch a group of Object A's from Core Data via an NSFetchedResultsController. For one particular object in this group, I know that it has only one Object B related to it and I want to retrieve the Object C.

I'm trying to do that like this:

NSArray *bArray = [objectA.relationA allObjects];
ObjectB *myB = bArray[0];
ObjectC *myC = myB.relationB;
(I've also tried [myB valueForKey:@"relationB"] with the same result)

The problem is that I can't get the fault to fire for Object C - I keep getting this for myC:

$6 = 0x0a947c00 (entity: ObjectC; id: 0xa9680b0 ; data: )

I'm passing this value on to another view controller and it's still a fault when it's accessed there, which isn't terribly useful.

It seems silly to have to refetch when I have the object, but I don't know what else to do. All the threads I can find on this say that faults are normal and that they will be fired when you access the faulted object, but that doesn't seem to be happening here.

Any suggestions?

Update: I tried adding this:

[fetchRequest setRelationshipKeyPathsForPrefetching:@[@"relationA.relationB"]];

But it did not make any difference.

janineanne
  • 585
  • 7
  • 17

1 Answers1

0

The problem is that I can't get the fault to fire for Object C - I keep getting this for myC:

$6 = 0x0a947c00 (entity: ObjectC; id: 0xa9680b0 ; data: )

You haven't tried to access myC yet. myC will remain a fault until you use it somehow. From the docs:

Fault handling is transparent—you do not have to execute a fetch to realize a fault. If at some stage a persistent property of a fault object is accessed, then Core Data automatically retrieves the data for the object and initializes the object (see NSManagedObject Class Reference for a list of methods that do not cause faults to fire). This process is commonly referred to as firing the fault.

So, (assuming ObjectC has a name property) if you do something like:

NSString *name = myC.name;

you should find that the fault at myC fires and you'll automagically have a real object to work with.

All the threads I can find on this say that faults are normal and that they will be fired when you access the faulted object, but that doesn't seem to be happening here.

They're right. Unless there's more that you haven't told us, it sounds like you're just expecting the fault to fire at a different time, i.e. when you assign the object to myC. But again, the fault won't fire until you do something to the fault, like getting or setting a property.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I tried that and it seems to work most of the time, but I still got this error once when accessing myC: "CoreData: error: NULL _cd_rawData but the object is not being turned into a fault" Always something new to learn… – janineanne Sep 03 '13 at 04:44
  • There's some info about that error [in a question of the same name](http://stackoverflow.com/q/9225046/643383). Sounds like you're trying to fire the fault in a thread not associated with the context that owns the fault. If you must use the fault in a different thread, consider passing just the `objectID` and then getting the object in the destination thread's context using the context's `-objectWithID:` method. Or perhaps I'm reading too much into this... – Caleb Sep 03 '13 at 04:57