gpt4 book ai didi

iphone - 如何在新 View 类中的不同线程上加载 UIWebview?在调用对象的 presentModalViewController 之前?

转载 作者:行者123 更新时间:2023-11-29 13:35:44 25 4
gpt4 key购买 nike

查看崩溃报告....

This GDB was configured as "x86_64-apple-darwin".Attaching to process 1798.
2012-05-17 13:19:59.628 PDFView[1798:11603] bool _WebTryThreadLock(bool), 0x684b940: 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...
1 WebThreadLock
[Switching to process 1798 thread 0x11603]
2 -[UIWebView _webViewCommonInit:]
3 -[UIWebView initWithCoder:]
4 _decodeObjectBinary
5 _decodeObject
6 -[UIRuntimeConnection initWithCoder:]
7 _decodeObjectBinary
8 -[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:]
9 -[NSArray(NSArray) initWithCoder:]
10 _decodeObjectBinary
11 _decodeObject
12 -[UINib instantiateWithOwner:options:]
13 -[UIViewController _loadViewFromNibNamed:bundle:]
14 -[UIViewController loadView]
15 -[UIViewController view]
16 -[UIViewController viewControllerForRotation]
17 -[UIViewController _visibleView]
18 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:]
19 -[UIViewController presentViewController:withTransition:completion:]
20 -[UIViewController presentViewController:animated:completion:]
21 -[UIViewController presentModalViewController:animated:]
22 -[LoginPageVC loadVC]
23 __invoking___
24 -[NSInvocation invoke]
25 -[NSInvocationOperation main]
26 -[__NSOperationInternal start]
27 -[NSOperation start]
28 [Switching to process 1798 thread 0x11603]
__block_global_6
29 _dispatch_call_block_and_release
30 _dispatch_worker_thread2
31 _pthread_wqthread
sharedlibrary apply-load-rules all
Current language: auto; currently objective-c

我认为我收到此错误,这是由于在另一个线程而不是主线程上加载 webview 请求。我想在后台加载 WebView 以避免 UI 阻塞。

//调用其他 View Controller

{
ViewController* objVC=[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

NSString* PDFURLString = @"http://images.apple.com/xserve/pdf/L422277A_Xserve_Guide.pdf";

//creating diff thread.....

NSOperationQueue *queue = [NSOperationQueue new];

// create own NSInvocation Operation to call other function of other view controller class

NSInvocationOperation * operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadURL) object:PDFURLString];

// add operation to queue

[queue addOperation:operation];
[operation release];

[self presentModalViewController:objVC animated:YES];

}

//这是 ObjVC 调用的函数。

-(void)loadURl:(NSString*)urlString
{

[_indicator startAnimating];
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:_PDFURLString]];
NSLog(@"loading URL DATA");
//Store the Data locally as PDF File

NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

_filePath = [resourceDocPath stringByAppendingPathComponent:@"myPDF.pdf"];
NSLog(@"File Path:%@",_filePath);

[pdfData writeToFile:_filePath atomically:YES];


//Now create Request for the file that was saved in your documents folder

NSURL *url = [NSURL fileURLWithPath:_filePath];

NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[_webView setUserInteractionEnabled:YES];

_webView.scalesPageToFit = YES;

[_webView setDelegate:self];

[_webView loadRequest:requestObj];

}

请查看我的代码并建议在不同线程上加载 Web View 的最佳方法,同时 UI 响应。谢谢。

最佳答案

loadRequest: 方法已经异步执行,因此从主线程调用它不应在加载期间阻塞 UI。

关于iphone - 如何在新 View 类中的不同线程上加载 UIWebview?在调用对象的 presentModalViewController 之前?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10632938/

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