作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个将图像上传到远程服务器的单一 View 应用程序。当用户提交上传时,我使用第二个 View Controller ,它显示为自定义弹出窗口并显示文件上传进度。上传完成后,自定义弹出窗口将展开到第一个 viewController。我使用 notification-obersever 模式在两个 viewController 之间进行通信。
我的问题如下:当我上传大文件时,可以观察到上传进度条,并且自定义弹出窗口在上传完成后展开。当我上传小文件时,上传时间非常快,即使文件上传成功,也无法观察到上传进度,并且自定义弹出窗口不会展开(请参见下面的代码)。
我不确定我是否正确设置了通知观察者模式?
第一个 View Controller :
Alamofire.upload(
multipartFormData: { multipartFormData in
for(key, value) in sendParamters{
multipartFormData.append((value.data(using: .utf8)!), withName: key)
}
for fileURL in arrayURLToUpload{
print("fileURL: \(fileURL)")
multipartFormData.append(fileURL, withName: "file[]")
}
},
to: UPLOAD_URL,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
/**TRACK PROGRESS OF UPLOAD**/
upload.uploadProgress { progress in
print("ECNTERFILENAME: \(progress.fractionCompleted)")
let progress = progress.fractionCompleted
// NOTIFICATION - UPLOAD PROGRESS
NotificationCenter.default.post(name: notifcationName, object: nil, userInfo: ["progress": progress])
}
/***/
// 'responseJSON' FIRES WHEN UPLOAD IS COMPLETE
// IT ALSO TO FIRES WHEN THE IS INTERNET CONNECTIVITY ISSUES
upload.responseJSON { response in
// NOTIFICATION UPLOAD COMPLETE
NotificationCenter.default.post(name: uploadFinishName, object: nil, userInfo: ["response": response])
}
case .failure(let encodingError):
print("UPLOAD ERROR")
print(encodingError)
}
}
)
第二个 View Controller :
override func viewDidLoad() {
super.viewDidLoad()
self.createUploadObserver()
self.createUploadFinishObserver()
if txtLabelOutlet == nil{ // WHEN IMAGE('tickok') HAS REPLACED 'txtLabelOutlet'
reformTxtLabel()
}
self.infoLabelOutlet.textColor = UIColor.black // CAN BE RED WHEN NETWORK CONNECTION IS LOST
self.progressOutlet.progress = 0
}
func createUploadObserver(){
print("createUploadObserver ..")
NotificationCenter.default.addObserver(forName: notifcationName, object: nil, queue: OperationQueue.main) { (notification) in
guard
var arrayN = notification.userInfo as? [String:Double],
let num = arrayN["progress"] else{
print("no valid data")
return
}
// UPDATE PROGRESS BAR
let progress = Float.init(num)
self.progressOutlet.progress = progress
// UPDATE LABEL
let percent = Int(progress*100)
print("percent: \(percent)")
if let progressOut = self.txtLabelOutlet{
print("OUTLET EXISTS!")
progressOut.text = "\(percent)%"
}else{
print("OUTLET DOES NOT EXIST")
}
}
}
func createUploadFinishObserver(){
print("createUploadFinishObserver ..")
NotificationCenter.default.addObserver(forName: uploadFinishName, object: nil, queue: OperationQueue.main) { (notification) in
print("notification.userInfo: \(notification.userInfo)")
guard let response = notification.userInfo as? [String: DataResponse<Any>] else{
print("Error completion response")
return
}
guard
let respObj = response["response"] as? DataResponse<Any>,
let result = respObj.result as? Result<Any> else{return}
if let value = result.value{
print("value: \(value)")
}
if result.description == "SUCCESS"{
// UPLOAD COMPLETE
// SHOW VIDEO CAPTURE
print("upload success")
if let progressOut = self.txtLabelOutlet{
print("OUTLET EXISTS - REMOVE FROM SUPERVIEW")
progressOut.removeFromSuperview()
}else{
print("OUTLET DOES NOT EXIST to remove from superview")
}
// ADD GREEN TICK IMAGE TO SHOW UPLOAD SUCCESS PROGRAMTICALLY
let image = UIImage.init(named: "tickok")
self.showCompleteTickImage(image: image!, completion: {
// DELAY EXECUTION OF DIMISS POPUP BY 2 SECONDS
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
print("DISMISS PROGRESS POPUP")
// DISMISS POUP AND SHOW IMAGE CAPTURE AGAIN
self.performSegue(withIdentifier: "inwindme2", sender: self)
}
})
}else{
// HANDLE UPLOAD FAIL
print("upload fail")
self.infoLabelOutlet.text = "Network Connection Lost"
self.infoLabelOutlet.textColor = UIColor.red
}
}
}
最佳答案
可能是交换
self.createUploadFinishObserver()
self.createUploadObserver()
由于上传小文件时的popupVC似乎尚未注册,因此完成帖子发生在观察之前,也可以在该弹出窗口完成后开始上传,
self.present(popup,animated:true) {
// start upload
}
如果继续
self.performSegue(/////
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
// start upload
}
通知中心要工作,您的应用程序逻辑必须 100% 确定帖子发生在观察之后
关于ios - 文件上传进度的通知观察者模式仅在文件很大时才有效 - 快速?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53640470/
我是一名优秀的程序员,十分优秀!