我的代码:
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...
答案 0 :(得分:1)
您的错误确切地告诉您问题是什么This may be a result of calling to UIKit from a secondary thread.
在您的createWebView方法中,您调用UIKit。当您没有在主线程上运行时,这是不允许的,在此代码示例中,您从另一个线程调用该方法。
为什么不将对该方法的调用移动到主线程上的调度?
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;
});
});