title: Alamofire uploading large files with progress and show progress on app restart
body: <p>I have setup the project with following steps. </p>
  
  <ol>
  <li>I had enable External accessory communication and Background Fetch</li>
  <li>AppDelegate:</li>
  </ol>
  
     <pre class="lang-swift prettyprint-override"><code>func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -&gt; Void) {
          API.sharedInstance.backgroundCompletionHandler = completionHandler
      }
  </code></pre>
  
  <ol start="3">
  <li>And here is the API.swift class which im handling all the api calls</li>
  </ol>
  
  <pre class="lang-swift prettyprint-override"><code>public class API: NSObject {
  
      var sessionManager: SessionManager
      var delegate: SessionDelegate
      public var backgroundSessionManager: Alamofire.SessionManager
  
      var backgroundCompletionHandler: (() -&gt; Void)? {
          get {
              return backgroundSessionManager.backgroundCompletionHandler
          }
          set {
              backgroundSessionManager.backgroundCompletionHandler = newValue
          }
      }
  
      /// access a shared instance of an API manager
      public class var sharedInstance: API {
          struct Singleton {
              static let instance = API()
          }
          return Singleton.instance
      }
  
      public required override init() {
          let configuration = URLSessionConfiguration.default
          self.sessionManager = SessionManager(configuration: configuration)
          let identifier = "com.appname.backgroundtransfer"
          self.backgroundSessionManager = Alamofire.SessionManager(configuration: URLSessionConfiguration.background(withIdentifier: identifier))
          self.sessionManager.adapter = AccessTokenAdapter()
          delegate = self.backgroundSessionManager.delegate
      }
  
      func authHeaderWithToken() -&gt; [String : String] {
          return [Constants.HeaderKeys.Authorization : "Bearer " + (AppManager.sharedInstance.accessToken ?? "")]
      }
  
      func uploadFilesToServer(parameters: [String : Any], image: UIImage?, audio: Data?, video: Data?, uploadItem: Upload, progressHandle: @escaping ProgressHandler, success: @escaping SuccessJsonClosure, failed: @escaping NetworkError) {
          let URL = Constants.BASE_URL + "api_uploadFiles"
  
          let headers: HTTPHeaders = [
              Constants.HeaderKeys.Authorization : "Bearer " + (AppManager.sharedInstance.accessToken ?? ""),
              "Content-type": "multipart/form-data"
          ]
  
          self.backgroundSessionManager.upload(multipartFormData: { (multipartData) in
              // multipart setup
              if let img = image {
                  multipartData.append(UIImageJPEGRepresentation(img, 1.0)!, withName: "file", fileName: "\(uploadItem.fileName!)", mimeType: "image/jpeg")
              }
  
              if let vid = video {
                  multipartData.append(vid, withName: "file", fileName: "\(uploadItem.fileName!)", mimeType: "video/mov")
              }
  
              if let aud = audio {
                  multipartData.append(aud, withName: "file", fileName: "\(uploadItem.fileName!)", mimeType: "audio/mov")
              }
  
              for (key, value) in parameters {
                  multipartData.append("\(value)".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!, withName: key)
              }
  
  
          }, to: URL, method: .post, headers: headers, encodingCompletion: { encodingResult in
              // transmission closure
              switch (encodingResult) {
              // encodingResult success
              case .success(let request, let streamingFromDisk, let streamFileURL):
  
                  // upload progress closure
                  request.uploadProgress(closure: { (progress) in
                      print("upload progress: \(progress.fractionCompleted)")
                      progressHandle(progress)
                  })
  
                  // response handler
                  request.responseJSON(completionHandler: { response in
                      switch response.result {
                      case .success(let jsonData):
                          // do any parsing on your request's response if needed
                          let json = try! JSON(data: response.data!)
                          let isOK = json["success"].boolValue
                          if isOK {
                              success(true, json)
                          } else {
                              failed(nil, json)
                          }
  
                      case .failure(let error):
                          failed(error, nil)
                      }
                  })
  
              // encodingResult failure
              case .failure(let error):
                  failed(error, nil)
  
              } // end encodingresult switch
          })
  
      }  
  }
  </code></pre>
  
  <p>Files get uploaded successfully to server if the app is not killed. but if i kill the app in middle of uploading, i can't get the progress status. And completion block not get executed when app is restart (kill and open). uploadFilesToServer function get call multiple times (Ex: 5 videos uploading with 5 calls). I need to track the progress for each uploading when the app restarted. How can we track the ongoing request? and how we can upload the large files and run a completion call for each request. </p>
  
  <p>In here im saving the videos to documents directory and pass the Data with url </p>