【问题标题】:Is this right way to update the value on main thread for UI change?这是更新主线程值以进行 UI 更改的正确方法吗?
【发布时间】:2015-12-24 15:32:37
【问题描述】:

在 iOS 中,我正在尝试在 uiwebview 中更新谷歌图表。我正在使用 dispatch_async(dispatch_get_global...) 来解析 json 信息。在异步调用中,我使用 dispatch_async(dispatch_get_main_queue...) 在主线程上执行 UI 元素。我仍然在屏幕上发现一些延迟,但没有 UI 挂起。请帮助我如何以更好的方式编写以下代码,以便一旦我从解析的信息中获得价值,我应该能够立即生成图表。下面是代码:

dispatch_queue_t syncInAsync=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);   
dispatch_async(syncInAsync, ^{        
[self parse:url];    
     dispatch_async(dispatch_get_main_queue(), ^{           
 NSString* htmlString = [NSString stringWithFormat:@"<html><head><script type=\"text/javascript\" src=\"https://www.google.com/jsapi\"></script><script type=\"text/javascript\">google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});google.setOnLoadCallback(drawChart);function drawChart() {var data = google.visualization.arrayToDataTable([['Reports', 'Value'],['Total',     %@ ]]);var options = {title: 'Total Reports'};var chart = new google.visualization.PieChart(document.getElementById('piechart'));chart.draw(data, options);}</script></head><body><div id=\"piechart\" style=\" text-align: left width: 400px; height: 200px;\"></div></body></html>",number];           
            [_web1 loadHTMLString:htmlString baseURL:nil];
    });
});

【问题讨论】:

  • 能否请您出示您的parse 方法。

标签: ios json parsing asynchronous grand-central-dispatch


【解决方案1】:

使用 sync 而不是异步线程更新 UI。 试试这个 -

 dispatch_queue_t syncInAsync=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(syncInAsync, ^{ [self parse:url]; dispatch_sync(dispatch_get_main_queue(), ^{ NSString* htmlString = [NSString stringWithFormat:@"<html><head><script type=\"text/javascript\" src=\"https://www.google.com/jsapi\"></script><script type=\"text/javascript\">google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});google.setOnLoadCallback(drawChart);function drawChart() {var data = google.visualization.arrayToDataTable([['Reports', 'Value'],['Total', %@ ]]);var options = {title: 'Total Reports'};var chart = new google.visualization.PieChart(document.getElementById('piechart'));chart.draw(data, options);}</script></head><body><div id=\"piechart\" style=\" text-align: left width: 400px; height: 200px;\"></div></body></html>",number]; [_web1 loadHTMLString:htmlString baseURL:nil]; }); }); 

【讨论】:

    【解决方案2】:

    在此块中写下您要更改的内容

       [[NSOperationQueue mainQueue] addOperationWithBlock:^{
    //type here
      }];
    

    【讨论】:

      【解决方案3】:

      你的做法大多是正确的。我想做一些改变:

      我想让 parse 方法执行代码以在 UI 上呈现。所以,像这样传递一个完成块。在解析过程中显示加载叠加层总是一个好主意:

      - (void)parse:(NSString *)iURL withCompletionBlock:MyCompletionBlock)iCompletionBlock {
         // Parse JSON here. Show Loading Overlay.
      
         // Once response is available
          dispatch_async(dispatch_get_main_queue(), ^{
              // Remove Loading Overlay.
      
              // Execute completion block
              completionBlock();
          });
      }
      

      这就是你的调用者的样子:

      dispatch_queue_t syncInAsync=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
      
      dispatch_async(syncInAsync, ^{
          [self parse:url withCompletionBlock:^{
          NSString* htmlString = [NSString stringWithFormat:@"<html><head><script type=\"text/javascript\" src=\"https://www.google.com/jsapi\"></script><script type=\"text/javascript\">google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});google.setOnLoadCallback(drawChart);function drawChart() {var data = google.visualization.arrayToDataTable([['Reports', 'Value'],['Total',     %@ ]]);var options = {title: 'Total Reports'};var chart = new google.visualization.PieChart(document.getElementById('piechart'));chart.draw(data, options);}</script></head><body><div id=\"piechart\" style=\" text-align: left width: 400px; height: 200px;\"></div></body></html>",number];
          [_web1 loadHTMLString:htmlString baseURL:nil];
      }];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-04
        • 2013-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-25
        • 2013-06-07
        • 2013-05-05
        相关资源
        最近更新 更多