2016-09-30 27 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") 
      }