ios - 后台线程的行为有所不同,具体取决于调用方法的时间

标签 ios multithreading core-data uisearchbar nsfetchedresultscontroller

在 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);

}

问题是,当我从 viewWillAppear 调用 prefetch 方法时,一切都很好,但是当我从编辑 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 内容应该在主线程上执行,但我看不出错误在哪里。

有什么指点吗?提前致谢!

最佳答案

我相信这可能是由于访问搜索栏的文本引起的。我在后台线程方法之一上遇到了类似的警告,我将问题追溯到搜索栏的文本方法。我注意到你也在这样做。

如果这是您的情况,警告的一个简单解决方案是在主线程上获取搜索栏的文本。

它确实解决了我的问题,但我真的想知道为什么会出现警告。一段时间以来,我一直试图在网络和 Apple 文档中找到有关此问题的信息,但还没有成功。

关于ios - 后台线程的行为有所不同,具体取决于调用方法的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11251430/

相关文章:

ios - 我的 iOS 应用程序如何为一系列颜色设置动画?

ios - 将 .pdf 和 .doc 文件提取到驻留在 iOS 设备中的应用程序中

java - `Thread.checkAccess()`是 `Thread.suspend()`的适当替代品吗?

swift 核心数据 : accessing relationship is slower than just fetching by predicate - is it designed that way?

ios - 如何测试 iOS 升级场景?

javascript - 将 'Core Data' 与 Phonegap 一起使用?

ios - 如何本地化全局字符串?

ios - 在 Cocos2dx iOS 项目中添加 Google Firebase

java - 静态方法和静态 block 之间的线程安全

c++ - 多维数组初始化: Any benefit from Threading?