質問

I have a core data relationship that looks like this

ItemA ->> ItemB

where as itemA has many of itemB. i wanted to use a fetched property that allowed me to grab all associated of itemB that were associated with itemA that had an int32 status property set as "2". So i created a fetched property in the data modeler that had the following:

fetched property: completedItem predicate: status == 2 destination: itemB

when i first tried it out, i got items back and i thought that was all cool and done, then later i noticed odd behavior and when i looked closer the items that it returned had nothing to deal with actual amount of itemB that was associated with an itemA object. Even weirder is that the return type is NSFaultingMutableArray. Here's a quick example

  • ItemA has 0 of itemB
  • a filtered predicate search on the NSSet property ItemA has of ItemB returns 0
  • the fetched property "completedItem" returns 4 of ItemB
  • the type it returns is NSFaultingMutableArray

This is just weird in my head right now and really isn't making sense. any ideas?

UPDATE 1:

it appears the fetched property listed here gets all ItemB objects that core data has to offer that matches the predicate, even if it's not associated with the ItemA in question

役に立ちましたか?

解決

Here's the answer to this all the weirdness in this issue:

1) The fetched properties indeed were not returning ItemB objects just for ItemA. In order for this to occur, you have to add something like this in the fetched properties predicate

status == 2 AND ItemA == $FETCH_SOURCE

2) From the Fetched Properties documentation:

A fetched property is evaluated lazily, and is subsequently cached.

If objects in the destination entity are changed, you must reevaluate the fetched property to ensure it is up-to-date. You use refreshObject:mergeChanges: to manually refresh the properties—this causes the fetch request associated with this property to be executed again when the object fault is next fired.

so basically use refreshObject:mergeChanges to manually refresh the object to reload the fetched property. you can do this by adding a refresh method or by doing some fancy overriding to the KVC get method inside your subclassed NSManagedObject.

This being said, others here (Rob Booth, Grady Player), have other valid solutions by bypassing fetched properties entirely. While these are faril

他のヒント

Kyle, the problem is that Core Data is trying to be smarter than you. Core Data doesn't want to fill up your memory with all of the object in your datastore if you aren't going to use them. So instead of creating real objects all of the time it creates "Faults". Faults are placeholders for real objects, and when you ask for those objects Core Data goes and populates the objects at that time.

My best guess is that your search on the ItemA.ItemB property isn't realizing the linked objects and so that is why you are getting back a 0 set. If you just did a NSSet *mySet = ItemA.ItemB I'm sure you'd see that mySet contains the correct count of objects. But doing a count on ItemA.ItemB is sending the count message to a Faulted Set and thus returns 0.

When you use your completedItems property CoreData seems to be doing at least something to return you the correct number of objects, but not the actual object data yet.

lets use this terminology:

we have one objectA of ClassA

we have some objects (unnamed) of ClassB

we have added the the ClassB objects to objectA.

objectA is a reference to a subclass of NSManagedObject... when you call objectA.completeItem, you are asking objectA for the set of ItemB's, this will work because you are working with one instance in memory.

when you make a new NSManagedObjectContext and perform a fetch in it, it doesn't know anything about your objectA or its relationships, unless you perform a save on objectA's context before you perform a fetch.

Edit:

for you predicate to filter out only the classB objects that are owned by ItemA, you can use a predicate like (assume itemA to be the inverse of your ItemA cpmletedItem relationship)

[NSPredicate predicateWithFormat: @"itemA = %@",itemA];

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top