7

There is an object A and an Object B. Object B has one attribute that is type transformable (image), and one relationship, which is to an object A. Object A may have a relationship to one, and only one, object B, or it may not.

As I enumerate through my object A array, I want to check if each object A has an object B. But, I don't want to fire the fault for object B (which would invoke the reverse imageToData NSValueTransformer). I just want to know if it is there or not. How can I do this without bringing object B into memory?

SAHM
  • 4,078
  • 7
  • 41
  • 77

2 Answers2

8

I think you can just test

if (objectA.relationshipToB != nil) ...

This will not fire a fault for the related B object because you don't access its properties.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks, that makes sense. I thought that objectA.relationshipToB would cause an automatic fault but I am glad to see that it does not. – SAHM Nov 16 '13 at 14:14
0

In Swift I got a

Could not find an overload for '!=' that accepts the supplied arguments

error. My relationship was correctly marked as optional but in the generated NSManagedObject my @NSManaged property didn't have a ? after it. So this check against nil failed. I added the ? and then I could check for existence of the relationship.

Before (didn't work)

@NSManaged var myRelationShip: MyClass

After (worked)

@NSManaged var myRelationShip: MyClass? // <--- Added `?`
disco crazy
  • 31,313
  • 12
  • 80
  • 83