目前我正在尝试同时加载大约4到5个不同的loadRequest,但我得出的结论是一次只能加载一个webView。虽然我已经为每个webview创建了自定义类,但loadRequest似乎不会被调用,而如果有另一个webview的loadRequest正在使用中。
有没有办法将这些调用放在不同的线程中,使其在分派机制中工作或利用?只是想知道有没有别的选择。
谢谢
发布于 2012-12-06 06:05:19
使用NSUrlConnection预加载数据,不使用线程
//for EACH url to load
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:tweet[@"profile_image_url"]]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue] // one should ideally use a different queue here to free main thread and ONLY do the imageView.image setting in Main thread
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if(!error) {
//load webview! with Data maybe!
}
}];发布于 2012-12-06 05:44:32
由于您似乎只有4或5个web视图,因此在调度队列中调用它们并不是那么低效。如果有100个请求,那么你需要考虑一些策略。
NSArray *myWebSites = [NSArray arrayWithObjects:@"www.cnn.com",@"www.fox.com",....,nil];
for(url in myWebsites){
dispatch_async(dispatch_get_global_queue, ^{
// get the data from the url.
// do the display in main queue
dispatch_async(dispatch_get_mainqueue, ^{
//load the web view;
}
}
}https://stackoverflow.com/questions/13732878
复制相似问题