magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.8k stars 1.8k forks source link

Sort keys "ascending" parameter gets overriden #1245

Closed ghost closed 5 years ago

ghost commented 8 years ago
+ (NSFetchRequest *) MR_requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context
{
    NSFetchRequest *request = [self MR_requestAllInContext:context];
    if (searchTerm)
    {
        [request setPredicate:searchTerm];
    }
    [request setFetchBatchSize:[self MR_defaultBatchSize]];

    NSMutableArray* sortDescriptors = [[NSMutableArray alloc] init];
    NSArray* sortKeys = [sortTerm componentsSeparatedByString:@","];
    for (__strong NSString* sortKey in sortKeys)
    {
        NSArray * sortComponents = [sortKey componentsSeparatedByString:@":"];
        if (sortComponents.count > 1)
          {
              NSString * customAscending = sortComponents.lastObject;
              ascending = customAscending.boolValue;
              sortKey = sortComponents[0];
          }

        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:ascending];
        [sortDescriptors addObject:sortDescriptor];
    }

    [request setSortDescriptors:sortDescriptors];

    return request;
}

This method modifies initial ascending parameter for all other elements in the array. You can easily reproduce this by specifying key:NO,anotherKey and passing YES to ascending parameter in MR_requestAllSortedBy. The result sort descriptor will contain ascending:NO for anotherKey.

To fix this you need to declare a local variable for custom ascending parameter and use it instead

deanWombourne commented 7 years ago

Hey. If no-one else is looking at this it seems a nice easy one for me to get the hang of contributing to this library :)

deanWombourne commented 7 years ago

Hey @dmazurenko - I can't reproduce this with a test :( Can you take a look at my PR and let me know what I've missed?

Thanks.

deanWombourne commented 7 years ago

Hey @dmazurenko - I was being dense, I've worked out what I missed, and I think my PR fixes it now :) Can you confirm please?

Thanks.

Coeur commented 5 years ago

Pull Request merged. Thank you @deanWombourne.

deanWombourne commented 5 years ago

Awesome, thanks!