在 CoreDataTableViewController(由斯坦福大学的人完成的子类)上,我从 sqlite 数据库中获取数据。
在模拟器和设备上提取速度非常快,但为了使其对旧设备友好,我想在后台执行提取并在完成时添加一个微调器。所以我添加了一个预取方法。整个事情看起来像这样:
-(void)setupFetchedResultsController{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:self.entity];
request.propertiesToFetch=[NSArray arrayWithObject:self.attribute];
request.resultType=NSDictionaryResultType;
request.returnsDistinctResults=YES;
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"%K != nil", self.attribute];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"%K != ''", self.attribute];
NSPredicate *predicate3= [NSPredicate predicateWithFormat:@"%K contains[cd] %@", self.attribute, self.seachBar.text];
NSArray *prepredicateArray;
if ([self.seachBar.text length]) {
prepredicateArray = [NSArray arrayWithObjects:predicate1, predicate2, predicate3,nil];
}else {
prepredicateArray = [NSArray arrayWithObjects:predicate1, predicate2,nil];
}
request.predicate=[NSCompoundPredicate andPredicateWithSubpredicates:prepredicateArray];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:self.attribute ascending:YES ]];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self performFetch];
}
-(void)prefetch{
UIView *translucentView=[[UIView alloc]initWithFrame:CGRectMake(110, 102, 100, 100)];
[translucentView setCornerRadius:7.0];
translucentView.backgroundColor=[UIColor blackColor];
translucentView.alpha=0.65;
UIActivityIndicatorView *spinner=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.frame=CGRectMake(0, 31.5, 100, 37);
[translucentView addSubview:spinner];
[spinner startAnimating];
[self.view addSubview:translucentView];
dispatch_queue_t fetchQueue = dispatch_queue_create("fetch stuff", NULL);
dispatch_async(fetchQueue, ^{
[self setupFetchedResultsController];
dispatch_async(dispatch_get_main_queue(), ^{
[translucentView removeFromSuperview];
});
});
dispatch_release(fetchQueue);
}
问题是,当我从一切调用该prefetch
方法时,viewWillAppear
一切都很好,但是当我从编辑 searchBar 时调用的方法调用它(在 searchBar 上键入时,获取动态显示获取的结果)时,它会给出以下错误:
void _WebThreadLockFromAnyThread(bool), 0x6b9b730: Obtaining the web lock from a thread other than the main thread or the web thread. UIKit should not be called from a secondary thread.
我的理解是 UI 的东西应该在主线程上执行,但我看不到错误在哪里。
任何指针?提前致谢!