NSArray returned by MR_findAllSortedBy will be cleared automatically #686

yorkwang opened this Issue Feb 27, 2014 · 2 comments

2 participants


It's strange, my code is as below:

//do search
NSPredicate *predicate;
NSString *searchStr =  [NSString stringWithFormat:@"*%@*",searchText];
predicate = [NSPredicate predicateWithFormat:@"name like[cd] %@ OR author like[cd] %@ ",searchStr,searchStr];
_comicsArrayBySearch = [Comic MR_findAllSortedBy:@"name" ascending:YES withPredicate:predicate inContext:localContext];

_comicsArrayBySearch is the datasource of a tableView, after every search, the search results will be shown on the tableview, every thing is fine. But without scrolling the tableView for a while, _comicsArrayBySearch’s core data item will be cleared automatically, then scroll tableview agin, tableView's cell count is still right, but every cell is blank with no data filled. Before using MagicalRecord, the search result array wasn't cleared automatically, but MagicalRecord is really easy and excellent.


Can you verify that the _comicsArrayBySearch instance variable still contains the data you expect it to?


Hi, tonyarnold:

Sorry for my late reply!

Here is my code,

  • (void)doSearch:(NSString*)searchText

    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];

    //do search
    NSPredicate *predicate;
    predicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@ OR author contains[cd] %@ ",searchText,searchText];
    _comicsArrayBySearch = [Comic MR_findAllSortedBy:@"name" ascending:YES withPredicate:predicate inContext:localContext];

    NSMutableArray *temp = [_comicsArrayBySearch mutableCopy];

    [temp sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    Comic *comic1 = (Comic *)obj1;
    Comic *comic2 = (Comic *)obj2;

    NSRange range1 = [comic1.name rangeOfString:searchText options:NSCaseInsensitiveSearch];
    NSRange range2 = [comic2.name rangeOfString:searchText options:NSCaseInsensitiveSearch];
    NSRange range3 = [comic1.author rangeOfString:searchText options:NSCaseInsensitiveSearch];
    NSRange range4 = [comic2.author rangeOfString:searchText options:NSCaseInsensitiveSearch];
    NSArray *temp = [NSArray arrayWithObjects:[NSNumber numberWithInt:range1.location],[NSNumber numberWithInt:range2.location ],[NSNumber numberWithInt:range3.location],[NSNumber numberWithInt:range4.location], nil];
    NSInteger min = [[temp valueForKeyPath:@"@min.integerValue"] integerValue];
    NSInteger minIndex = [temp indexOfObject:[NSNumber numberWithInt:min]];
    //minIndex is comic1
    if (minIndex == 0 || minIndex == 2) {
        return NSOrderedAscending;
    return NSOrderedDescending;


    _comicsArrayBySearch = temp;

    [self updateUiAfterSearch];


If I call doSearch method in background, _comicsArrayBySearch's NsMangaObjects will be automatically cleared after a while.

NSString *searchText = searchBar.text;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self doSearch:searchText];

If I call doSearch method in main thread, _comicsArrayBySearch's NsMangaObjects will not be cleared.

BTW, NSFetchedResultsController is not used because I want to sort the search results in my own way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment