代码之家  ›  专栏  ›  技术社区  ›  Farzad

如何在多个视图中使用来自singleton的一个回调

  •  0
  • Farzad  · 技术社区  · 7 年前

    onCompleted 在多个类视图中。

    import Foundation
    import Alamofire
    
    class AudioSyncManager {
    
        //var onDownloadStart: (()->())?
        var onDownloadFinished: ((_ isSuccess: Bool)->())?
        var onDownloadProgress: ((_ progress: Float)->())?
    
        static let shared = AudioSyncManager()
    
        private var downloadRequest: DownloadRequest?
        private var isDownloading = false
    
        var listData: [MainModel] = []
    
        func doDownloding(onStarted: @escaping ()->()) {
    
           if listData.count == 0 || isDownloading {
                return
           }
    
            let firstModel = listData.first
            if checkMp3FileExists(model: firstModel!) {
    
                self.isDownloading = false
                self.listData.removeFirst()
    
                if self.listData.count > 0 {
                    self.doDownloding {}
                }
    
                return
    
            }
    
            let mp3URLString = MyHelper.MEDIA_URL_PREFIX + (firstModel?.link)!
            let url = URL(string: mp3URLString)
    
            let destination = DownloadRequest.suggestedDownloadDestination(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask)
    
            //isDownloading = true
            onStarted()
    
            downloadRequest = Alamofire.download(url!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil, to: destination)
                .downloadProgress { (progress) in
    
                self.onDownloadProgress?(Float(progress.fractionCompleted))
    
                }.response { (response) in
    
                    self.isDownloading = false
                    self.onDownloadFinished?(true)
    
                    if self.listData.count > 0 {
                        self.listData.removeFirst()
                    }
    
                    if self.listData.count > 0 {
                        self.doDownloding{}
                    }
    
            }
    
        }
    
        func addSingleTask(mainModel: MainModel) {
    
            listData.append(mainModel)
            doDownloding{}
    
        }
    
        func addListTask(newList: [MainModel]) {
    
            listData.append(contentsOf: newList)
            doDownloding{}
    
        }
    
    }
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   OOPer    7 年前

    一个例子 onCompleted 并将其作为 doDownload 方法

    class Service {
         let static shared = Service()
    
         func doDownload(onCompleted: (()->())?) {
              //...
              onCompleted?()
         }
    }
    
        2
  •  2
  •   Abhinav Jha    7 年前

    第1点

    你应该在下面一行得到错误

         let static shared = Service()
    

    因为static关键字应该排在声明之后。

    static let shared = Service()
    

    第2点

        func doDownload(onCompleted: @escaping ()->()) {
        onCompleted()
        }
    

    如下调用函数

        let service = Service.shared
        service.doDownload { () in
            print("Called in completion Handler")
        }
    

    Closures

        3
  •  1
  •   Ashish    7 年前