gpt4 book ai didi

ios - GCD 错误 :Tried to obtain the web lock from a thread other than the main thread or the web thread

转载 作者:行者123 更新时间:2023-11-29 03:22:39 25 4
gpt4 key购买 nike

我的代码:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *prePath = [[spineArray objectAtIndex:spineIndex - 1] spinePath];
NSURL *preURL = [NSURL fileURLWithPath:prePath];
UIWebView *tmpWebView = [self createWebView:preURL];
dispatch_async(dispatch_get_main_queue(), ^{
self.preWebView = tmpWebView;
});

});

- (UIWebView *)createWebView:(NSURL *)url
{
UIWebView *tmpWebView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0,kRootViewWidth, kRootViewHeight)] autorelease];
tmpWebView.delegate = self;
[tmpWebView setBackgroundColor:[UIColor whiteColor]];


currentTextSize = 100;

UISwipeGestureRecognizer *rightSwipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(gotoNextPage)];
[rightSwipeRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft];

UISwipeGestureRecognizer *leftSwipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(gotoPrevPage)];
[leftSwipeRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];

[tmpWebView addGestureRecognizer:rightSwipeRecognizer];
[tmpWebView addGestureRecognizer:leftSwipeRecognizer];

[rightSwipeRecognizer release];
[leftSwipeRecognizer release];

[tmpWebView loadRequest:[NSURLRequest requestWithURL:url]];
return tmpWebView;

}

我们运行时,提示错误为:

Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

最佳答案

您的错误准确地告诉您您的问题是什么这可能是从辅助线程调用 UIKit 的结果。 在您的 createWebView 方法中,您调用了 UIKit。当您不在主线程上运行并且在此代码示例中您从另一个线程调用该方法时,这是不允许的。

为什么不将该方法的调用移动到主线程上的分派(dispatch)中?

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *prePath = [[spineArray objectAtIndex:spineIndex - 1] spinePath];
NSURL *preURL = [NSURL fileURLWithPath:prePath];
dispatch_async(dispatch_get_main_queue(), ^{
UIWebView *tmpWebView = [self createWebView:preURL];
self.preWebView = tmpWebView;
});

});

关于ios - GCD 错误 :Tried to obtain the web lock from a thread other than the main thread or the web thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20895473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com