Weird behaviour using joins #192

elbryan opened this Issue Jun 26, 2012 · 4 comments


None yet

3 participants

elbryan commented Jun 26, 2012

This is not an issue but it's rather a question. Since I don't know whether this is a MagicaRecord bug, iOS bug, Core Data bug or me making mistakes (this is sure) I would like to ask here since you know things better.

This happened after installing the latest iOS version (the beta one) and I don't know whether it worked correctly on the previous version.

This is the workflow:

  • I use always the MR_contextForCurrentThread to create and fetch resources in my background thread
  • The thread is mainly a loop cycle. At each interaction I call the MR_save method (also on the MR_contextForCurrentThread)
  • When the thread has done, the user can tap on the UI and get another view controller to be presented
  • This new VC fetches (therefore from the main thread, thus another context) the resources which should have been created
    by the aforementioned thread.

What happens:

  • The set of resources is empty
  • If I completely close the application, I reopen it and I tap the view to get the VC, every works just fine.

A small ugly workaround I found is to issue [[NSManagedObjectContext MR_defaultContext] reset] right before opening the VC (therefore fetching the objects from the main thread).

More specifically:
Company and Family are two NSManagedObject.

company = [Company MR_findFirstByAttribute:@"name" withValue:@"company_name"];

and its content:

<Company: 0xa3cc110> (entity: Company; id: 0x978ccc0 x-coredata:///Company/tD12DB62B-CF31-4143-87BA-1978FB9266EF2 ; data: {
families = (
"0xa4c0710 x-coredata:///Family/tD12DB62B-CF31-4143-87BA-1978FB9266EF74",
"0x978eaa0 x-coredata:///Family/tD12DB62B-CF31-4143-87BA-1978FB9266EF3"
name = company_name;

Notice the two Family objects.
Next I want to retrieve all the Family objects tied with such Company:

- (NSArray *) familiesInCompany:(Company *) company {
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"company == %@", company];
    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@" == %@",];

    NSArray *first = [Family MR_findAllSortedBy:@"name" ascending:YES withPredicate:predicate1];
    NSArray *second = [Family MR_findAllSortedBy:@"name" ascending:YES withPredicate:predicate2];
    NSArray *third = [Family MR_findByAttribute:@"company" withValue:company andOrderBy:@"name" ascending:YES];

    return second;

What happens is the following:

  • first is an empty array
  • second contains the two Family objects (correct)
  • third is an empty array

According to what the Apple documentation states (the Joins paragraph), it is strongly suggested to use the way I used in the first experiment but it won't work. Still, if I close and reopen the application, all the three statements give the same result: an array with two entries.

Can someone shed a light on this? Thank you so much and sorry for the long post.


Having a similar problem, but I am not running the beta, so I suspect it is unrelated to that for you. I'm now using your suggestion of resetting the context when I'm re-populating my NSFetchedResultsController, and now getting my desired behavior, so thanks - agree, not a pretty work-around, but it will do until a fix comes to MR.


Resetting the context introduced other problems for me that I didn't notice at first. The latest code in combination with the fix linked to below and things seem to be working the way I want them to now if I call:

[[NSManagedObjectContext MR_defaultContext] MR_saveNestedContexts];

in the Completion block.



Given the age of this issue, and the volume of issues we have to work through, I've decided to close this alongside a number of other older issues.

If you can still replicate the issue under the latest in-development version of MagicalRecord (3.0 at the time of writing), please feel free to re-open and one of @magicalpanda/team-magicalrecord will take another look. Thanks!

@tonyarnold tonyarnold closed this Apr 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment