//func upload...//...let request: NSMutableURLRequest = AFhttpRequestSerializer().multipartFormRequestWithMethod("POST",URLString: url,parameters: s3parameters,constructingBodyWithBlock: { formData in let data: AFMultipartFormData = formData data.appendPartWithfileURL(fileURL,name: "file",error: nil) },error: nil) request.setValue("application/Json",forhttpheaderFIEld: "Accept") let sessionManager: AFURLSessionManager = AFURLSessionManager(sessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration()) var progress: nsprogress? let uploadTask: NSURLSessionUploadTask = sessionManager.uploadTaskWithStreamedRequest(request,progress: &progress,completionHandler: { (response,responSEObject,error) in //... }) uploadTask.resume() progress?.addobserver(self,forKeyPath: "fractionCompleted",options: NSkeyvalueObservingOptions.Initial,context: nil)//...overrIDe func observeValueForKeyPath(keyPath: String,ofObject object: AnyObject,change: [NSObject : AnyObject],context: UnsafeMutablePointer<VoID>) { let progress: nsprogress = object as nsprogress println("progress: \(progress.fractionCompleted)")}
文件是正确上传的,但事实并非如此 – 我只打印了一次进度值(“progress:0.0”),然后我以AFURLSessionManager.m中的EXC_BAD_ACCESS结束:
#pragma mark - NSURLSessionTaskDelegate- (voID)URLSession:(__unused NSURLSession *)session task:(__unused NSURLSessionTask *)taskdIDSendBodyData:(__unused int64_t)bytesSenttotalBytesSent:(int64_t)totalBytesSenttotalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend{ self.progress.totalUnitCount = totalBytesExpectedToSend; <<< EXC_BAD_ACCESS self.progress.completedUnitCount = totalBytesSent;}
我究竟做错了什么?我应该设置“var progress:nsprogress?”不同还是还有别的东西我不见了?
解决方法 哦,我已经修好了 – 我需要从进度中移除观察者:let uploadTask: NSURLSessionUploadTask = sessionManager.uploadTaskWithStreamedRequest(request,error) in//...progress?.removeObserver(self,context: nil) // added this line//...})总结
以上是内存溢出为你收集整理的uploadTask使用Swift中的AFNetworking进度全部内容,希望文章能够帮你解决uploadTask使用Swift中的AFNetworking进度所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)