首页 > 解决方案 > 即使用户移动到任何 ViewController 并回来,如何使用 MZDownloadManager 更新特定的单元格标签

问题描述

我正在使用 MZDownloadManger 库使用 url 下载文件,除标签更新外,一切正常,当我开始下载时,它更改为“开始下载”,然后开始其进度,如 10% 20% 等。它工作正常但当我移动到任何其他视图控制器时,它的进度会停止并且不会将标签值更新为“已下载”。我在我的本地数据库'0'和'1'中设置了一个标志,0表示未下载,1表示已下载。

这是用户选择单元格并点击下载时的代码:

func keepOfflineFiles(sender: UIButton) {
    if files[sender.tag].onLocal == "1"{
        self.displayAlert(title: AlertTitle.alert, message: AlertMsg.alreadyDownloaded)
    } else{
        if self.files[sender.tag].status == "on amazon"{
            let indexPath = IndexPath.init(row: sender.tag, section: 0)
            let cell = self.tblFilesPro.cellForRow(at: indexPath)
            if let cell = cell {
                let downloadCell = cell as! filesTableViewCell
                downloadCell.lblDetailsPro.text = "Starting Download. . ."
            }

            let pathString:String = ""
            let fileName:String = self.files[sender.tag].file_id! + self.files[sender.tag].Extension!

            if(fileName != ""){
                let local_url = NSURL(fileURLWithPath: pathString.getDocumentsPath())
                let filePath = local_url.appendingPathComponent(fileName)?.path
                let fileManager = FileManager.default
                if fileManager.fileExists(atPath: filePath!) {
                    // FILE AVAILABLE
                    let indexPath = IndexPath.init(row: sender.tag, section: 0)
                    let cell = self.tblFilesPro.cellForRow(at: indexPath)
                    if let cell = cell {
                        let downloadCell = cell as! filesTableViewCell
                        downloadCell.lblDetailsPro.text = "Downloaded"
                    }
                    self.fileid = self.files[sender.tag].file_id!
                    self.updateFileStatusToRealm()
                    //self.displayAlert(title: AlertTitle.alert, message: AlertMsg.alreadyDownloaded)
                } else {
                    // FILE NOT AVAILABLE
                    let completeUrl:String = Tray.downloadURLBasePath + self.files[sender.tag].fileLink!
                    if(self.verifyUrl(urlString: completeUrl)) {
                        self.fileid = self.files[sender.tag].file_id!
                        let index:String = String(sender.tag)
                        self.AppDelegateObj.downloadManager.addDownloadTask(fileName as String, fileURL: completeUrl as String, destinationPath: index as String)
                    } 
                }
            }
        }else{
            self.displayAlert(title: AlertTitle.alert, message: AlertMsg.inArchiveProcess)
        }

    }
}

这是我在 AppDelegate 中调用的 MZDownloadManager 的代表 来更新进度

func downloadRequestDidUpdateProgress(_ downloadModel: MZDownloadModel, index: Int) {
    let root : UINavigationController = self.window?.rootViewController as! UINavigationController
    if let master = root.topViewController as? TabBarController {
        if let nav = master.viewControllers?[0] as? FilesVC {
            nav.refreshCellForIndex(downloadModel, index: Int(downloadModel.destinationPath)!)
        }
    } else {
        print("Somthing went wrong while downloading this file.")
    }
}

下载完成时

func downloadRequestFinished(_ downloadModel: MZDownloadModel, index: Int) {

    let root : UINavigationController = self.window!.rootViewController! as! UINavigationController
    if let master = root.topViewController as? TabBarController {
        if let nav = master.viewControllers![0] as? FilesVC{
            nav.getDownloadingStatusOfCellForIndex(downloadModel, index: Int(downloadModel.destinationPath)!)
        }
    } else {
        print("Somthing went wrong while finishing downloading of this file.")
    }
}

刷新单元格标签的方法

 func refreshCellForIndex(_ downloadModel: MZDownloadModel, index: Int) {
    let indexPath = IndexPath.init(row: index, section: 0)
    let cell = self.tblFilesPro.cellForRow(at: indexPath)
    if let cell = cell {
        let downloadCell = cell as? filesTableViewCell
        downloadCell?.updateCellForRowAtIndexPath(indexPath, downloadModel: downloadModel)
    }
}

获取单元格并更改值的方法

func getDownloadingStatusOfCellForIndex(_ downloadModel: MZDownloadModel, index: Int) {
    let indexPath = IndexPath.init(row: index, section: 0)
    let cell = self.tblFilesPro.cellForRow(at: indexPath)
    if let cell = cell {
        let downloadCell = cell as? filesTableViewCell
        downloadCell?.lblDetailsPro.text = "Downloaded"
        self.fileid = self.files[index].file_id!
        self.updateFileStatusToRealm()
    }
}

这是在数据库中将标志值 0 更改为 1 的方法:

func updateFileStatusToRealm(){
    let fileToUpdate = uiRealm.objects(filesDataTable.self).filter("file_id = %@", self.fileid)
    let realm = try! Realm()
    if let file = fileToUpdate.first {
        try! realm.write {
            file.onLocal = "1"
            tblFilesPro.reloadData()
        }
    }
}

标签: iosswifturldownloadswift4

解决方案


推荐阅读