2016-08-19 23 views
0

如果網絡不好,我的應用程序在進入前景時卡住圖標大約4-5秒。在幾乎所有主要的加載頁面中,我都從服務器獲取圖像。我沒有任何關於applicationDidEnterBackground()applicationWillEnterForeground()緩慢的應用程序進入前景

在這裏,在主視圖控制器的代碼,我有圖像的集合視圖:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(homeReuseIdentifier, forIndexPath: indexPath)as! HomeViewCell 

    // Reload the collection view after insert item at indexPath 
    cvHome.reloadItemsAtIndexPaths([indexPath]) 

    let entry = dataCell.infoCell[indexPath.row] 
    cell.backImageView.image = nil 

    let stringURL = "https://.....\(entry.filename)" 

    let image = UIImage(named: entry.filename) 
    cell.backImageView.image = image 

    if cell.backImageView.image == nil { 
     if let image = dataCell.cachedImage(stringURL) { 
      cell.backImageView.image = image 
     } else { 
      request = dataCell.getNetworkImage(stringURL) { image in 
       cell.backImageView.image = image 
      } 
     } 
    } 

    cell.titleLabel.text = entry.title 
    cell.authorLabel.text = entry.author 

    // Fix the collection view cell in size 
    cell.contentView.frame = cell.bounds 
    cell.contentView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] 

    // To edit the cell for deleting 
    cell.editing = editing 

    return cell 
} 

這是getNetworkImage功能與Alamofire:

func getNetworkImage(urlString: String, completion: (UIImage -> Void)) -> (ImageRequest) { 
    let queue = decoder.queue.underlyingQueue 
    let request = Alamofire.request(.GET, urlString) 
    let imageRequest = ImageRequest(request: request) 
    imageRequest.request.response(
     queue: queue, 
     responseSerializer: Request.imageResponseSerializer(), 
     completionHandler: { response in 
      guard let image = response.result.value else { 
       return 
      } 
      let decodeOperation = self.decodeImage(image) { image in 
       completion(image) 
       self.cacheImage(image, urlString: urlString) 
      } 
      imageRequest.decodeOperation = decodeOperation 
     } 
    ) 
    return imageRequest 
} 
+1

是同步或異步的數據讀取? – Harsh

+0

我正在使用Alamofire,所以它是異步 – Giovanni

回答

0

我不知道,如果你的網絡代碼很糟糕,因爲你沒有提供任何代碼。

我的猜測是,提取數據花費的時間太長,而且會阻止用戶界面。所以目前,你的應用程序是同步的。所以任務一次執行一個。你的用戶界面被阻止的原因是因爲它必須等待網絡完成。

您需要做的是在後臺執行抓取操作,並始終將您的用戶界面保留在主隊列中。

0

如果您要調用Synchronous,則必須等到任務完成後才能完成。我們可以在完成當前任務後開始下一個任務。從服務器獲取映像時,請確保您使用的是同步還是不同步。您不必等待第二次使用異步服務。您無需等待從服務器獲取圖像。從服務器獲取圖像時可以執行其他任務。

對於這個GCD是一個很好的使用。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 
    // Do the back ground process here 
    ......Fetch the image here 
    dispatch_async(dispatch_get_main_queue(), ^{ 
    // Update the UI here 
    .....Do your UI design or updation design part here 
    }); 
}); 

全局隊列

異步任務將在這裏進行。 它不會等待。 異步函數不會阻止當前正在執行的線程繼續執行下一個函數。

主隊列

我們必須始終訪問主線程UI套件類。

If you don't want to wait a seconds,go with GCD

+0

我實際上使用Alamofire和AlamofireImages應該獲取異步。 – Giovanni

+0

我編輯了我的問題,包括代碼。 – Giovanni