2016-08-19 8 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 
} 

をし、これがAlamofireとgetNetworkImage機能である:

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

私がもしわかりませんあなたは何も提供していないので、ネットワーキングコードが悪いです。

私の推測では、データの取得には時間がかかりすぎ、UIがブロックされているようです。現在、あなたのアプリは同期しています。そのため、タスクは一度に1つずつ実行されます。あなたのUIがブロックされる理由は、ネットワーキングが完了するのを待たなければならないからです。

あなたがする必要があるのは、バックグラウンドでフェッチを実行し、常にUIをメインキューに保持することです。

0

同期を呼び出す場合は、タスクが終了するまで待たなければなりません。現在のタスクが完了したら、次のタスクを開始できます。サーバーからイメージを取得するときは、同期を使用しているかどうかを確認します。非同期でもう一度待ってはいけません。サーバからイメージをフェッチしている間は、待つ必要はありません。サーバからイメージをフェッチするときに、他のタスクを実行できます。

この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 Kitクラスにアクセスする必要があります。

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

+0

私は実際には非同期をフェッチすべきAlamofireとAlamofireImagesを使用しています。 – Giovanni

+0

コードを含めて私の質問を編集しました。 – Giovanni

関連する問題