【问题标题】:Tracking totalBytesSent blocks animations for a few seconds跟踪 totalBytesSent 会阻止动画几秒钟
【发布时间】:2016-04-21 09:38:16
【问题描述】:

我目前正在开发 Swift iPhone 应用中的上传功能。

在上传过程中,我想显示一个进度指示器。但是,任何需要刷新显示的代码命令似乎都不会在几秒钟之前执行。

例如使用下面的代码,当数据开始发送时,PROGRESS 会立即出现在控制台中,但顶视图会在大约 10 秒后消失。

    func URLSession(_ session: NSURLSession,
                  task task: NSURLSessionTask,
                       didSendBodyData bytesSent: Int64,
                                       totalBytesSent totalBytesSent: Int64,
                                                      totalBytesExpectedToSend totalBytesExpectedToSend: Int64) {
    //uploadProgressCallback(uploadProgressRatio: min(Double(totalBytesSent) / self.uploadedFileTotalSize, 1.0) )

    print("PROGRESS")
    let topController:UINavigationController = UIApplication.sharedApplication().keyWindow!.rootViewController as! UINavigationController
    topController.view.alpha = 0.0


}

知道为什么吗?

非常感谢您的帮助。

拉斐尔

【问题讨论】:

    标签: swift upload progress


    【解决方案1】:

    仅从主线程更新 UI 以获得高效性能:

    dispatch_async(dispatch_get_main_queue()) { [unowned self] in
        //update UI hear
    }
    

    【讨论】:

    • 或者,创建自定义 NSURLSession 调用主队列上的委托方法:let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: self, delegateQueue: NSOperationQueue.mainQueue())
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2013-01-06
    相关资源
    最近更新 更多