首页 > 解决方案 > iOS:为什么我的活动视图控制器(self)在使用downloadTask下载视频时采用单例(sharedInstance)的地址?

问题描述

当我启动应用程序时,我想获得正常工作的活动应用程序是用户的交互。其次,我想在后台,应用程序检查是否存在与用户先前添加但尚未处理的旧视频有关的过程。为此,我使用当前的活动控制器 ( self ) 和 ( sharedInstance ) 单例。
我的问题是,当我尝试在“活动模式”下下载视频时,self的地址,即活动视图,取单例的地址,我不明白为什么......
例如,当我使用应用程序时通常,地址是Printing description of self: 0x155d5b3a0

当我在 urlSession(URLSession, didFinishDownloadingTo location:) 函数中时,我的地址是单例 0x155e3e890 之一

主类:

@objcMembers class ViewControllerCamBox: UIViewController,URLSessionDelegate,URLSessionDownloadDelegate,FormViewViewControllerDelegate {
        static let sharedInstance = ViewControllerCamBox()
    ...

    }

    func stopVideo
    {
        ...
        self.url = fileUrls![0]
        self.downloadVideo(urlStr: self.url)
        ...
    }

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, did

WriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {

                if totalBytesExpectedToWrite > 0 {

                    if let onProgress = onProgress {
                        calculateProgress(session: session, completionHandler: onProgress)
                    }
                    let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
                    DispatchQueue.main.async {
                        if (self.progressLabel != nil && self.progressBar != nil)
                        {
                        self.progressBar.isHidden = false
                        self.progressBar.progress = Double(progress)
                        self.progressLabel.text = "Téléchargement en cours..."
                        self.progressLabel.isHidden = false
                    }
                }
                debugPrint("Progress \(downloadTask) \(progress)")
            }
        }

        func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
            debugPrint("Download finished: \(location)")

        let nameVideo = self.code_prospect_unique + "_video.MOV"
//MY PROBLEM => self has the sharedInstance address and not the self address of the current active viewcontroller.

        ...
        }

func downloadVideo(urlStr:String)
{
    //self.id = get id from query
    let url = URL(string: urlStr)!
    self.dataTask = self.activate().downloadTask(with: url)
    self.dataTask?.resume()
}

启动功能以检查是否有要发送的视频:

func checkGeneral()
    {
        //check download = 0e
        let query = "SELECT ID, MAIL, URL, CODE_PROSPECT_UNIQUE FROM VIDEO WHERE DOWNLOAD = 0"
        LocalDatabase.sharedInstance.methodToSelectData(query, completion: { (result) in
            if (result.count > 0)
            {
                let viewControllerCamBox = ViewControllerCamBox.sharedInstance
                if let resultFound = result[0] as? [String: Any] {
                    guard !resultFound.keys.contains("ErrorMessage")
                        else {return}
                    let id = resultFound["ID"] as! NSNumber
                    let url = resultFound["URL"] as! String
                    let code_prospect_unique = resultFound["CODE_PROSPECT_UNIQUE"] as! String
                    viewControllerCamBox.id = id.stringValue
                    viewControllerCamBox.code_prospect_unique = code_prospect_unique
                    viewControllerCamBox.downloadVideo(urlStr: url)
                    print("Data selected successfully: code prospect unique:\(String(describing: viewControllerCamBox.code_prospect_unique))")
                }

                //each rows send
                //check resized = 0

                //check upload = 0

            }
            else
            {
                print("Fail while data selection:  code prospect unique:\(self.code_prospect_unique)")
            }
        })

    }

有什么不好的吗?我只是想在后台使用单例和用户正常使用的活动视图。

提前致谢。

标签: iosswiftdownloadsingletonnsurlsession

解决方案


推荐阅读