2017-02-15 82 views
0

我正在使用Swift作爲iOS開發人員,最近我在使用異步方式添加子視圖時遇到了一些麻煩,我正在使用dispatch_async(dispatch_get_main_queue(),但它仍然沒有工作,子視圖不是一個接一個地實時添加,但是我必須等到調用完成後UI纔會更新。我的目標是逐個添加子視圖,而for循環是實時的仍在繼續,下面是我的代碼的一部分。如果有人能幫助我,我真的很感激它。視圖不是異步添加子視圖Swift

for i in 0 ..< self.allChannels.count { 
     let param = Params().getEpgParams(String(self.allChannels[i].channelNumber))  
     uNetwork().httpRequestNoLoading(self.utilityClass.currentServer , parameters: param, method: uNetwork.METHOD.POST, parent: self) { (epgResponse, extra_information) ->() in 

      if(extra_information != uNetwork.EXTRA_INFORMATION.SUCCESS){ 
       self.presentViewController(self.utilityClass.alert("Error", message: "An Unexpected Error Happened"), animated: true, completion: nil) 
      } else if(epgResponse == nil){ 
       self.presentViewController(self.utilityClass.alert("Error", message: "Couldn't get any response from the server"), animated: true, completion: nil) 
      } else { 
       do { 
        var titleArray = [String]() 
        var start = [String]() 
        var end = [String]() 
        var progressArray = [Float]() 

        let jsonObject = try NSJSONSerialization.JSONObjectWithData(epgResponse, options: []) as! NSDictionary 
        if let response_object = jsonObject["response_object"] as? [[String: AnyObject]] { 
         for menu in response_object{ 
          titleArray.append(menu["title"] as! String) 
          start.append(menu["programstart"] as! String) 
          end.append(menu["programend"] as! String) 
          progressArray.append(menu["progress"] as! Float) 
         } 



         if(titleArray.count > 0){ 
          currentTitle = titleArray[0] 
          currentStart = start[0][start[0].startIndex.advancedBy(12)..<start[0].startIndex.advancedBy(12+4)] 
          currentEnd = end[0][end[0].startIndex.advancedBy(12)..<end[0].startIndex.advancedBy(12+4)] 
          progressValue = Float(progressArray[0]/100) 
         } else { 
          currentTitle = "Programet e \(self.allChannels[i].title)" 
          currentStart = "00:00" 
          currentEnd = "00:00" 
          progressValue = 0.5 
         } 

          dispatch_async(dispatch_get_main_queue(), { 
          indiSub[i].removeFromSuperview() 

          self.contentView = UIView(frame: CGRectMake(0, ((self.scrollView.frame.height/4) * CGFloat(i)), self.epgView.frame.width, self.scrollView.frame.height/4)) 
          self.contentView.layer.masksToBounds = true 
          self.scrollView.addSubview(self.contentView) 

          let button = UIButton(frame: CGRectMake(0, ((self.scrollView.frame.height/4) * CGFloat(i)), self.epgView.frame.width, self.scrollView.frame.height/4)) 
          button.tag = Int(self.allChannels[i].channelNumber) 
          button.addTarget(self, action: #selector(LiveTvTest.playFromEpg(_:)), forControlEvents: .TouchUpInside) 
          self.scrollView.addSubview(button) 

          let currentTime = UILabel(frame: CGRectMake((self.scrollView.frame.height/4) + 25, 0, 60, self.contentView.frame.height/3)) 
          if currentStart.characters.count == 4{ 
           currentTime.text = currentStart + "  |" 
          } else if (currentStart.characters.count == 5){ 
           currentTime.text = currentStart + " |" 
          } 
          currentTime.font = UIFont(name: "Helvetica Neue", size: 15) 
          currentTime.adjustsFontSizeToFitWidth = true 
          currentTime.textColor = UIColor(red:0.54, green:0.13, blue:0.13, alpha:1.0) 
          self.contentView.addSubview(currentTime) 

          let nextTime = UILabel(frame: CGRectMake((self.scrollView.frame.height/4) + 25, currentTime.frame.height, 60, self.contentView.frame.height/3)) 
          if nextStart.characters.count == 4{ 
           nextTime.text = nextStart + "  |" 
          } else if (nextStart.characters.count == 5) { 
           nextTime.text = nextStart + " |" 
          } 
          nextTime.font = UIFont(name: "Helvetica Neue", size: 15) 
          nextTime.adjustsFontSizeToFitWidth = true 
          nextTime.textColor = UIColor(red:0.53, green:0.53, blue:0.53, alpha:1.0) 
          self.contentView.addSubview(nextTime) 
          }) 


        } else { 
         print("something went wrong with response object") 
        } 




       } catch { 
        self.presentViewController(self.utilityClass.alert("Error", message: "Something went wrong, please try again later"), animated: true, completion: nil) 
       } 
      } 
     } 
    } 

回答

0

如果你能保證你的閉合(即您在交給httpRequestNoLoading)是runni ng在任意線程中,您可以使用dispatch_sync您的UI更新到主線程;這將一直等到工作項目執行完畢。但是如果close已經在主線程中運行,那肯定會出現死鎖。

Btw。您還應該將presentViewController調用派送到主線程中。

0

我猜想問題是你正在設置一組http調用。這些調用並行執行並通過回調返回。那麼你可以將幾個視圖更新非常緊密地結合在一起,因此,即使代碼是串行的,更新也會連續發生。

也就是說,另一個要記住的是,OSX/IOS批量他們的UI更新。所以這可能是因爲你在mainQueue上做了很多工作,沒有機會看到工作發生。爲什麼不嘗試使用dispatch_after添加一些隨機延遲來查看是否會讓事情在不同時間流行?