magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.79k stars 1.79k forks source link

Weird behaviour using joins #192

Closed franzhcs closed 10 years ago

franzhcs commented 12 years ago

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:

What happens:

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:@"company.name == %@", company.name];

    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:

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.

robreuss commented 12 years ago

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.

franzhcs commented 12 years ago

There is also sort of discussion here: http://stackoverflow.com/questions/11212702/nspredicate-not-executed/11407413#11407413

robreuss commented 12 years ago

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.

https://github.com/clinseman/MagicalRecord/commit/8c777c1d01e9550e19ea284206b9658a97ea3cf3#-P0

tonyarnold commented 10 years ago

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!