0

In my Core Data model I have two entities: "Photo" and "Tag". The relationship between these is like : Photo << ------->> Tag.

When I insert a Photo into database and setup the relationship, then I want to fetch an NSArray of photos using an entity "Tag". The code is:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY tags == %@)", tag];
requestForPhoto.predicate = predicate;
NSSortDescriptor *sortD = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
requestForPhoto.sortDescriptors = [NSArray arrayWithObject:sortD];
NSArray *matches = [document.managedObjectContext executeFetchRequest:requestForPhoto error:nil];

the number of matches is 0; then I close the app, open it again and execute the upper code, the number turns normal, becoming 1.

I'm sure that I execute method saveToURL: forSaveOperation: to save the data.
I NSLog the content of database before and after re-open app, It works fine, the relationship has already established.

I have another Core Data model:

photo <<---------> location
when I fetch a photos, I use the NSString of location(not an entity) to do so. the results is very regular.

asheeshr
  • 4,088
  • 6
  • 31
  • 50
Ding Ding
  • 295
  • 3
  • 7
  • After you insert the new photo do you save it before doing the fetch? – lnafziger Apr 17 '12 at 14:57
  • @lnafziger Yes. After insert a photo, I execute the method ' [document saveToURL:document.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:NULL];' I NSLog My database, and it works – Ding Ding Apr 17 '12 at 23:43
  • http://stackoverflow.com/questions/9437159/core-data-managed-object-does-not-see-related-objects-until-restart-simulator This page is very helpful to this issue and my problem solved – Ding Ding Apr 18 '12 at 00:58

0 Answers0