2016-09-30 26 views
0

我想從Firebase中下載幾個帖子並將它們顯示在我的桌面視圖中。在下面的代碼中,我注意到tableview只在從preLoadImage函數調用下載了每個圖像後才加載。我在另一個dispatch_async調用中調用整個loadDataFromFirebase()調用。我在這裏做錯了什麼?不應該preLoadImage函數在後臺隊列和tableview立即加載運行嗎?dispatch_async和從雲中迅速下載圖像

現在我從日誌中看到這一點,所以我可以告訴所有1+ MB圖像先下載,然後加載tableview。感謝 Downlading圖像 Downlading圖像 Downlading圖像 Downlading圖像 Downlading圖像 Downlading圖像 Downlading圖像 Downlading圖像 Downlading圖像 Downlading圖像 在TableView中

// loadDataFromFirebase is being called from another dispatch_async call 
func loadDataFromFirebase() { 

     print("Global: Loading data from Firebase") 
     // code to retrieve data.     

     self.globalPost.insert(userPost, atIndex: 0) 
//helps imageloading -- this is the one that causes the delay 
     self.preLoadImage(userPost.userImage) 
     print ("Obtained DATA in loadDataFromFirebase") 


     } 




// image loading - this is called in a background queue but until the download here doesn't finish my tableview does not load. 

    func preLoadImage(image: String) { 
     if (image != "") { 
      dispatch_async(dispatch_get_main_queue(), { 
       UIImage.cachedImageWithURL(image) 
       print ("Downlading image") 

     }) 


     } 

    } 
+0

想知道是否有辦法限制後臺下載的圖片數量,而不是下載所有的圖片。 – user3470200

回答

0

使用這種在preLoadImage()固定它。這看起來是否正確?

if (image != "") { 
      dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) { 
       UIImage.cachedImageWithURL(image) 
       print ("Downlading image") 
      }