2017-01-13 7 views
1

SCENARIOGCD:連続したダウンロード呼び出しを作成する。 ONE

アプリのダウンロードユーザーのサブスクリプションを一つずつBY ONE。この呼び出しは、複数の場所で行われます(別のネットワーク呼び出しの後に完了ブロックで、UIAlertControllerからのボタンを押すと)。ロジックはすべてのサブスクリプションをダウンロードすることです.1つのサブスクリプションのダウンロードが終了すると、すべてのダウンロードが完了してからSVProgressHUDが終了するまで次のサブスクリプションのダウンロードが終了します。このコードは、Xcodeからビルドして実行するときに効果的です。我々はIPAを構築し、当社の顧客に送信する場合でも、このロジックは、ストールのいくつかの並べ替えを作成し、SVProgressHUDアラートが回転し続ける:

enter image description here

我々のアプリケーションがダウンロードコンテンツの周りに集中しているので、これは大きな問題ですサブスクリプションから。

私はそれをアーカイブしてビルドした後にこのロジックがストールするのですが、ではなく、をビルドしてXcodeから実行すると、

以下に、コード:

// Making the call 

    DispatchQueue.main.async { 
     DGWebService().syncUserSubscribedContent { 
      DispatchQueue.main.async { 
       self.finishLogin() 
      } 
     } 
    } 

// Sequentially going through each subscription and downloading them 

    func syncUserSubscribedContent(completion: @escaping Constants.WebService.ContentCompletion) { 
     let subscriptions = MPTUser.sharedUser.getSubscriptionsForDownload() 
     DispatchQueue.global().async { 
      if subscriptions.count > 0 { 
       var index:Int = 0 
       var t = subscriptions.count 
       var downloading: Bool = false 
       while t != 0 { 
        if downloading == false { 
         downloading = true 
         if index < 0 { 
          index = 0 
         } 
         if index > subscriptions.count - 1 { 
          index = subscriptions.count - 1 
         } 
         if index <= subscriptions.count { 
          let subscription = subscriptions[index] 
          if subscription.didDownloadContent == false { 
           if let subscriptionID = subscription.subscriptionID { 
            DispatchQueue.main.async { 
             SVProgressHUD.show(withStatus: "Downloading Documents\nfor\n\(subscription.functionalGroupName!)\n\(index+1) of \(subscriptions.count)") 
            } 
            self.getUserSubscribedContent(subscriptionID: subscriptionID, completion: { (success) in 
             subscription.didDownloadContent = true 
             index += 1 
             t -= 1 
             downloading = false 
            }) 
           } 
           else { 
            index += 1 
            t -= 1 
            downloading = false 
           } 
          } 
         } 
         else { 
          index += 1 
          t -= 1 
          downloading = false 
         } 
        } 
       } 
      } 
      completion() 
     } 
    } 

self.getUserSubscribedContentは、コンテンツをダウンロードし、バックブロックで完了を送る機能です。

誰かが私をここで助けることができたら、それは非常に感謝します。

+0

あなたの例ではたくさんのコードがあるようです。バックグラウンドスレッド(ダウンロードを同期させる)で 'DispatchGroup'を実行し、ダウンロードごとにメインスレッドのラベルを更新してみましたか? –

+0

こんにちは@ILikeTau。これは有望な提案です。あなたが私にコードの例を与えることができれば、それは非常に高く評価されるでしょう。 –

答えて

1

DispatchGroupをお試しください。ここには、おおまかな(まだテストされていない)例があります:

DispatchQueue.global().async { 
    let subscriptions = MPTUser.sharedUser.getSubscriptionsForDownload() 
    let group = DispatchGroup() 
    var completed = 0 
    let completion: (Bool) -> Void = { 
     if $0 { 
      completed += 1 
     } 
     group.leave() 
     DispatchQueue.main.async { 
      SVProgressHUD.show(withStatus: "Downloading Documents\nfor\n\(subscription.functionalGroupName!)\n\(completed) of \(subscriptions.count)") 
     } 
    } 
    for subscription in subscriptions { 
     self.getUserSubscribedContent(subscriptionID: subscription.subscriptionID, completion: completion) 
     group.enter() 
    } 
    // However long you want to wait (in seconds) before timing out 
    _ = group.wait(timeout: .now() + 30) 
} 
関連する問題