1

我在一些较旧的设备(例如 ipod touch 4th gen)上遇到 UI 卡顿/暂停,我已将其缩小到这段代码:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"gender != %@ && catId != %@", [NSNumber numberWithInt:0], [NSNumber numberWithInt:7]];
NSSet *filteredCats = [cats filteredSetUsingPredicate:predicate]; //cats is an NSSet

如果我注释掉这两行代码并改为这样做:

NSSet *filteredCats = cats;

表演非常流畅。那么如何在不导致屏幕更新短暂停顿的情况下改进这种过滤呢?

4

1 回答 1

2

您可以异步进行过滤,这应该有助于提高性能:

 __weak ViewController *bSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul),^{

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"gender != %@ && catId != %@", [NSNumber numberWithInt:0], [NSNumber numberWithInt:7]];

    NSSet *filteredCats = [cats filteredSetUsingPredicate:predicate]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
        [bSelf updateMyViewWithFilteredSet:filteredCats]; 
    }
}
于 2013-04-22T17:42:42.227 回答