【问题标题】:concurrent way to load multiple loadRequest of webView并发方式加载webView的多个loadRequest
【发布时间】:2012-12-05 21:34:17
【问题描述】:

目前我尝试同时加载大约 4 到 5 个不同的 webView,但是我得出的结论是一次只能加载一个 loadRequest。虽然我为每个 webview 创建了自定义类,但 loadRequest 并没有被调用,而如果有另一个 webview 的 loadRequest 正在被使用。

有没有办法将这些调用保留在不同的线程中以使其工作或在调度机制中使用?只是想弄清楚是否有替代方案。

谢谢

【问题讨论】:

    标签: iphone objective-c ios ipad cocoa


    【解决方案1】:

    使用 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!
                                   }
                               }];
    

    【讨论】:

      【解决方案2】:

      由于您似乎只有 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;
              }
          }
      }
      

      【讨论】:

      • 不需要线程。 NSURLConnections 在常见的 runloop 上运行良好 :) 只是不做任何同步网络
      • 我在这里遇到了另一个问题。我通过委托将这个自定义 UIWebView 类的对象发送给另一个类。现在,我注意到 loadRequest 调用确实成功了,但是没有一个委托被调用,尽管我已将委托设置为 self。如果我在同一个类中实例化对象,我唯一一次注意到正在进行的调用。关于 loadRequest,我在这里遗漏了什么吗?谢谢。
      猜你喜欢
      • 1970-01-01
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多