2017-07-19 123 views
0

大家好我最近已將我的iOS應用程序遷移到Swift 3.1(Xcode 8.3.3)。我自己找出了大部分問題,但最後一個錯誤仍然困擾着我。那麼,削減長話短說,我用Alamofire庫進行調用Web服務和一些方法用於調度同步如下:Swift 3.1 Migration Dispatch.main.async模糊引用成員異步(執行:)'

class func createVideoActivity(_ type: Int, permission: Int, message: String, video: URL, progressview:UIProgressView , completion: @escaping (_ type: ResponseType , _ response : Int, _ message: String) -> Void) { 

     let user_id = CFunctions.getSession("id") 

     var serviceURL = baseURL + "&task=createActivity&user_id=\(user_id)&type=\(type)&permission=\(permission)" 

     serviceURL = serviceURL.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)! 
     let url = URL(string: serviceURL) 
     let authHeader = ["":""] 

     let mimetype = "video/mov" 

     var movieData: Data? 
     do { 
      movieData = try Data(contentsOf: URL(fileURLWithPath: (video.relativePath)), options: NSData.ReadingOptions.alwaysMapped) 
     } catch _ { 
      movieData = nil 
      return 
     } 
     let filename = "upload.mov" 

     upload(
      multipartFormData:{ multipartFormData in 
       multipartFormData.append(movieData!, withName: "filedata",fileName: filename,mimeType: mimetype) 
       multipartFormData.append(message.data(using: String.Encoding.utf8)!, withName: "message") 
      }, 
      to: url!, 
      headers: authHeader, 
      encodingCompletion: 
      { 
       encodingResult in 
       switch encodingResult { 
       case .success(let uploads, _, _): 

        .uploadProgress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in 

         DispatchQueue.main.async { 
          let percent = (Float(totalBytesWritten)/Float(totalBytesExpectedToWrite)) 
          print(percent) 
          progressview.setProgress(percent, animated: true) 
         } 

        } 
        uploads.validate() 
        uploads.responseJSON { serverResponse in 

         switch serverResponse.result { 

         case .success(let JSON): 

          debugPrint(JSON) 

          if (JSON as AnyObject).value(forKey: "status") as! Int == 1 { 
           completion(ResponseType.kresponseTypeSuccess,(JSON as AnyObject).value(forKey: "status") as! Int, (JSON as AnyObject).value(forKey: "response") as? String ?? "") 
          } else { 
           completion(ResponseType.kresponseTypeSuccess,(JSON as AnyObject).value(forKey: "status") as! Int, (JSON as AnyObject).value(forKey: "response") as? String ?? "") 


          } 

         case .failure(let error): 

          let dataString = String(data: serverResponse.data!, encoding: String.Encoding.utf8) 
          print("createVideoActivity Request failed with error: \(String(describing: dataString))") 
          completion(ResponseType.kResponseTypeFail, error as! Int, "Service failed") 

         } 

        } 
       case .failure(let encodingError): 
        print(encodingError) 
       } 
      } 
     ) // upload - end 
    } 

我越來越「曖昧參考成員‘異步(執行:)’」 Dispatch.main.sync行錯誤。你能找出發生了什麼事嗎?

+0

你試試這個:DispatchQueue.main.async(執行:{() - >在你的代碼無效}) – u84six

+0

是的u84six已經做到了這一點,沒有工作。雖然問題已解決,我立即在下面發佈答案。 – Ali

回答

1

的問題是不是與DispatchQueue.main.async但Alamofire的上傳進度語法,具有以下塊代替它,並得到了問題解決:

uploads.uploadProgress { (progress: Progress) in 
     DispatchQueue.main.async { 
       progressview.setProgress(progress.fractionCompleted, animated: true) 
     } 
}