应用程序在NSFetchRequests上无响应

时间:2012-04-30 20:05:47

标签: iphone ios core-data nsmanagedobjectcontext nsfetchrequest

Core Data Model

我的第一个iPad应用程序中有上面的CoreData模型。我正在TableViewController中构建一个过滤系统,如下所示。问题在于,每当我进行UI更改时,切换点击按钮的开关,我的UI将在一两秒钟内无响应。我运行一个非常长的函数来重新创建照片的获取请求,然后运行更多计数提取来确定是否应该启用控件。我只是不知道如何能够以一种有意义的方式将其分开,以防止挂起。即使我需要在一秒左右添加旋转视图,我也很满意。只是想摆脱滞后。

正如我所提到的,这是我第一次尝试iOS开发,所以我很感激任何建议......

enter image description here

-(void) refilterPhotos {
/*
*   First section builds the NSCompoundPredicate to use for searching my CoreData Photo objects. 
    Second section runs queries so 0 result controls can be disabled.
*/
subpredicates = [[NSMutableArray alloc] init];

NSPredicate *isNewPredicate;
if(newSwitch.on) {
    isNewPredicate = [NSPredicate predicateWithFormat:@"is_new == 1"];
} else {
    isNewPredicate = [NSPredicate predicateWithFormat:@"is_new == 0"];
}

[subpredicates addObject:isNewPredicate];

//Photo Types
PhotoType *photoType;
NSPredicate *photoTypePredicate;
for (UISwitch *photoSwitch in photoSwitches) {
     PhotoType * type = (PhotoType *) photoSwitch.property;
    if([type.selected boolValue] == YES) {
        NSLog(@"photo_type.label == %@", type.label);
        photoType = type;
        photoTypePredicate = [NSPredicate predicateWithFormat:@"photo_type.label == %@", type.label];
        break;
    }
}

//Feed Types
FeedType *feedType;
NSPredicate *feedTypePredicate;
for (UISwitch *feedSwitch in feedSwitches) {
    FeedType * type = (FeedType *) feedSwitch.property;
    if([type.selected boolValue] == YES) {
        NSLog(@"feed_type.label == %@", type.label);
        feedType = type;
        feedTypePredicate = [NSPredicate predicateWithFormat:@"feed_type.label == %@", type.label];
        break;
    }
}

//Markets
NSArray *filteredMarkets = [model.availableMarkets filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"selected == 1"]];
for (Market *market in filteredMarkets) {
    [subpredicates addObject:[NSPredicate predicateWithFormat:@"ANY markets.name == %@", market.name]];
}


//Tags
NSArray *filteredTags = [model.availableTags filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"selected == 1"]];
for (Tag *tag in filteredTags) {
    NSLog(@"ANY tags.name == %@",tag.name);
    [subpredicates addObject:[NSPredicate predicateWithFormat:@"ANY tags.name == %@", tag.name]];
}

if(photoTypePredicate)
    [subpredicates addObject:photoTypePredicate];
if(feedTypePredicate)
    [subpredicates addObject:feedTypePredicate];
NSPredicate *finished = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];//Your final predicate

model.availablePhotos = [model fetchPhotoswithPredicate:finished];
[[self parentViewController] setTitle:[NSString stringWithFormat:@"%d items",[model.availablePhotos count]]];

NSLog(@"FILTERED PHOTOS:::: %d", [model.availablePhotos count]);
[gridVC reloadGrid];   

/**
 *  Filtering Section Here, I'm running count requests for each grouping of controls to ensure if they're selected, results will be returned.
 *  If zero results, I'll disable that control. For the switch-based controls, I need to removed them before running my fetches since there can only be
 *  one switch value per photo.
 */



//Have to remove the existing type predicate since they're exlcusive values
[subpredicates removeObject:isNewPredicate];

//New Toggle
NSPredicate *newRemainderPredicate = [NSPredicate predicateWithFormat:@"is_new == %d",newSwitch.on?0:1];
[subpredicates addObject:newRemainderPredicate];

if([model countPhotoswithPredicate:[NSCompoundPredicate andPredicateWithSubpredicates:subpredicates]]<1) {
    [newSwitch setEnabled:NO];
} else {
    [newSwitch setEnabled:YES];
}

[subpredicates removeObject:newRemainderPredicate];
[subpredicates addObject:isNewPredicate];



[subpredicates removeObject:photoTypePredicate];
//Photo Type Toggles
NSArray *remainderPhotoTypes = [photoSwitches filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"on == NO"]];
for( UISwitch*control in remainderPhotoTypes) {
    PhotoType *remainderPhotoType = (PhotoType*)control.property;

    [subpredicates addObject:[NSPredicate predicateWithFormat:@"photo_type == %@", remainderPhotoType]];
    if([model countPhotoswithPredicate:[NSCompoundPredicate andPredicateWithSubpredicates:subpredicates]]<1) {
        //NSLog(@"PHOTOTYPE OFF %@", remainderPhotoType.label);
        control.enabled = NO;
    } else {
        //NSLog(@"PHOTOTYPE ON %@ count = %d", remainderPhotoType.label, [[model fetchPhotoswithPredicate:[NSCompoundPredicate andPredicateWithSubpredicates:subpredicates]] count]);
        control.enabled = YES;
    }
    remainderPhotoType.enabled = [NSNumber numberWithBool:control.enabled];
    [subpredicates removeObject:[NSPredicate predicateWithFormat:@"photo_type == %@", remainderPhotoType]];
}
if(photoTypePredicate)
[subpredicates addObject:photoTypePredicate];



[subpredicates removeObject:feedTypePredicate];
//Feed Type Toggles
NSArray *remainderFeedTypes = [feedSwitches filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"on == NO"]];
for( UISwitch*control in remainderFeedTypes) {
    PhotoType *remainderFeedType = (PhotoType*)control.property;

    [subpredicates addObject:[NSPredicate predicateWithFormat:@"feed_type == %@", remainderFeedType]];
    if([model countPhotoswithPredicate:[NSCompoundPredicate andPredicateWithSubpredicates:subpredicates]]<1) {
        control.enabled = NO;
    } else {
        control.enabled = YES;

    }
    remainderFeedType.enabled = [NSNumber numberWithBool:control.enabled];
    [subpredicates removeObject:[NSPredicate predicateWithFormat:@"feed_type == %@", remainderFeedType]];
}
if(feedTypePredicate)
[subpredicates addObject:feedTypePredicate];



NSArray *remainderMarkets = [[model availableMarkets] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"selected == 0"]];
//Markets..many-to-many so I don't remove the existing predicate
for( Market *remainderMarket in remainderMarkets) {
    [subpredicates addObject:[NSPredicate predicateWithFormat:@"ANY markets == %@", remainderMarket]];
     NSInteger countForTag = [model countPhotoswithPredicate:[NSCompoundPredicate andPredicateWithSubpredicates:subpredicates]];

    if(countForTag<1) {
        remainderMarket.enabled = [NSNumber numberWithInt:0];
    } else {
        remainderMarket.enabled = [NSNumber numberWithInt:1];
    }
    [subpredicates removeObject:[NSPredicate predicateWithFormat:@"ANY markets == %@", remainderMarket]];
}



NSArray *remainderTags = [[model availableTags] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"selected == 0"]];
//TAGS..many-to-many so I don't remove the existing predicate 
int tagCounter = 0;
for( Tag *remainderTag in remainderTags) {
    [subpredicates addObject:[NSPredicate predicateWithFormat:@"ANY tags == %@", remainderTag]];
    NSInteger countForTag = [model countPhotoswithPredicate:[NSCompoundPredicate andPredicateWithSubpredicates:subpredicates]];
    if(countForTag<1) {
        NSLog(@"TAG OFF %@", remainderTag.name);
        remainderTag.enabled = 0;
    } else {
        NSLog(@"TAG ON %@ count = %d", remainderTag.name, countForTag);
        remainderTag.enabled = [NSNumber numberWithInt:1];
    }
    [subpredicates removeObject:[NSPredicate predicateWithFormat:@"ANY tags.name == %@", remainderTag.name]];
    tagCounter ++;
} 
//Update the controls with this new data
[self.tableView reloadData];    

}

1 个答案:

答案 0 :(得分:1)

好的,这里有几件事需要考虑。

首先,我会考虑为主要搜索字段创建索引。没有索引,每次搜索都是线性的,因为它必须检查每条记录的值。索引将导致更快的搜索时间。

其次,我对在复合谓词中排序要非常小心。它将根据订单过滤它们。因此,您希望首先使您成为最快,最多的过滤谓词。尽可能快地修剪可能的解决方案空间。

通过索引前1-3个谓词中使用的属性,可以获得很多收益。我在底部注意到,当您查询计数时,您仍然使用相同的复合谓词。你真的想要吗?此外,在此代码中

//Have to remove the existing type predicate since they're exlcusive values
[subpredicates removeObject:isNewPredicate];
//New Toggle
NSPredicate *newRemainderPredicate = [NSPredicate predicateWithFormat:@"is_new == %d",newSwitch.on?0:1];
[subpredicates addObject:newRemainderPredicate];

您正在从正面移除is_new支票,并将其放在后面。如果您只是检查这个谓词以切换该开关,并且您只关心是否有0或更多,为什么甚至使用整个复合谓词?是&#34;切换&#34;相对于所有其他领域开/关?

如果继续这样做,请记住,它将首先执行所有其他谓词(有些是引用)。尽可能快地保持它们尽可能快地过滤。

第三,使用参考文献很方便,但价格昂贵。通过单独查询它们,然后使用复合谓词来过滤内存中的对象,可以获得更好的性能。

第四,您应该在一个单独的线程中执行所有这些查询。这很容易做到,但确切的方法取决于您当前的ManagedObjecttContext排列。你有一个MOC,一个父/子关系,一个UIManagedDocument?基本上,您可以创建单独的MOC,并调用performBlock来执行提取。实际上,您可以使用多个MOC同时异步触发所有这些提取。

然后,你可以在完成后调用主线程。

最后,您可能需要考虑对数据库进行非规范化。它会让你使用更多空间,但提取速度会快得多。具体来说,关系字段......您可以将照片/提要标签放入照片本身。这样,在搜索时,您不必进行额外的连接来获取这些记录。

所以,这不是一个简单的答案,而是实现其中的每一个,看看你的表现是否有相当大的提升(更不用说你的UI响应)。