取消上传请求时 Alamofire 3.5.1 崩溃

Alamofire 3.5.1 crashed when cancel the upload request

我只是按照cnoon's suggest取消请求,步骤如下:

  1. post the multipartFormData upload request;
  2. catch the upload progress and get the Alamofire.Request in the encodingCompletion;
  3. do cancel request;

那么崩溃就来了

所有上传相关代码如下: 私有变量请求:Alamofire.Request?

    func upload() {
        Alamofire.upload(
            .POST,
            "\(AppManager_Inst.currentServerAddress)/cloud/uploadFile",
            multipartFormData: { [unowned self] multipartFormData in

                if self.uploadFile.data.isKindOfClass(UIImage),
                    let image = self.uploadFile.data as? UIImage,
                    let imageData = UIImagePNGRepresentation(image) {
                    multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "\(self.uploadFile.name).png", mimeType: "image/png")
                }

                for (key, value) in parameters {
                    multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
                }

            }, encodingCompletion: { [unowned self] encodingResult in
                switch encodingResult {
                case .Success(let upload, _, _):

                    self.request = upload

                    upload.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
                        dispatch_async(dispatch_get_main_queue()) {
                            let buffer = ["file": "\(self.uploadFile.id)", "speed": "\(bytesRead)", "readed": "\(totalBytesRead)", "all": "\(totalBytesExpectedToRead)"]
                            GlobalBroadcast_Inst.broadcastMsg(.uploading, msg: buffer)
                        }
                    }
                    upload.responseData(completionHandler: { (response) in

                        self.uploading = false

                        let resp = NewFileResp.parseFromData(response.data!)
                    })

                case .Failure(let error):
                    self.uploading = false
                    print(error)
                }
            }
        )
    }

    request?.cancel()

崩溃信息如下:

*** Terminating app due to uncaught exception 'UninitializedMessage', reason: ''
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010ceb2d85 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010f322deb objc_exception_throw + 48
    2   ProtocolBuffers                     0x000000010bb72b69 -[PBGeneratedMessageBuilder checkInitialized] + 169
    3   CommonFoundation                    0x000000010bd1a17b -[NewFileRespBuilder build] + 43
    4   CommonFoundation                    0x000000010bd18942 +[NewFileResp parseFromData:] + 130
    5   QBCloud                             0x000000010b690fe0 _TFFFC7QBCloud8Uploader6uploadFT_T_U0_FOC9Alamofire7Manager31MultipartFormDataEncodingResultT_U0_FGVS1_8ResponseCSo6NSDataCSo7NSError_T_ + 448
    6   Alamofire                           0x000000010b92c0d7 _TTRXFo_oGV9Alamofire8ResponseCSo6NSDataCSo7NSError__dT__XFo_iGS0_S1_S2___dT__ + 151
    7   Alamofire                           0x000000010b92bc4d _TFFFC9Alamofire7Request8responseuRxS_22ResponseSerializerTyperFT5queueGSqPSo17OS_dispatch_queue__18responseSerializerx17completionHandlerFGVS_8Responsewx16SerializedObjectwx11ErrorObject_T__DS0_U_FT_T_U_FT_T_ + 797
    8   Alamofire                           0x000000010b92a494 _TPA__TFFFC9Alamofire7Request8responseuRxS_22ResponseSerializerTyperFT5queueGSqPSo17OS_dispatch_queue__18responseSerializerx17completionHandlerFGVS_8Responsewx16SerializedObjectwx11ErrorObject_T__DS0_U_FT_T_U_FT_T_ + 164
    9   Alamofire                           0x000000010b8e3bf7 _TTRXFo__dT__XFdCb__dT__ + 39
    10  libdispatch.dylib                   0x0000000110578d9d _dispatch_call_block_and_release + 12
    11  libdispatch.dylib                   0x00000001105993eb _dispatch_client_callout + 8
    12  libdispatch.dylib                   0x00000001105811ef _dispatch_main_queue_callback_4CF + 1738
    13  CoreFoundation                      0x000000010ce0c0f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    14  CoreFoundation                      0x000000010cdcdb99 __CFRunLoopRun + 2073
    15  CoreFoundation                      0x000000010cdcd0f8 CFRunLoopRunSpecific + 488
    16  GraphicsServices                    0x0000000113cd3ad2 GSEventRunModal + 161
    17  UIKit                               0x000000010dda5f09 UIApplicationMain + 171
    18  QBCloud                             0x000000010b692b42 main + 114
    19  libdyld.dylib                       0x00000001105ce92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type 

有人可以帮忙吗?

我发现问题的根本原因是 response.data 是 0 字节 NSData,所以 protobuf 的 parseFromData 失败:

upload.responseData(completionHandler: { (response) in

                        self.uploading = false

                        let resp = NewFileResp.parseFromData(response.data!)
                    })