Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在多个视图中使用来自singleton的一个回调_Ios_Swift_Singleton - Fatal编程技术网

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

Ios 如何在多个视图中使用来自singleton的一个回调,ios,swift,singleton,Ios,Swift,Singleton,如何在多个类视图中使用onCompleted 更新了完整代码 import Foundation import Alamofire class AudioSyncManager { //var onDownloadStart: (()->())? var onDownloadFinished: ((_ isSuccess: Bool)->())? var onDownloadProgress: ((_ progress: Float)->())?

如何在多个类视图中使用
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{}

    }

}
删除属性
onCompleted
并将其作为
doDownload
方法的参数的示例:

删除属性
onCompleted
并将其作为
doDownload
方法的参数的示例:


第1点

您应该在下面的行中得到错误

     let static shared = Service()
因为静态关键字应该排在声明之前

static let shared = Service()
第2点

使用完成处理程序实现onDownload函数

    func doDownload(onCompleted: @escaping ()->()) {
    onCompleted()
    }
如下调用函数

    let service = Service.shared
    service.doDownload { () in
        print("Called in completion Handler")
    }
有关闭包的更多详细信息,请访问以下链接。
第1点

您应该在下面的行中得到错误

     let static shared = Service()
因为静态关键字应该排在声明之前

static let shared = Service()
第2点

使用完成处理程序实现onDownload函数

    func doDownload(onCompleted: @escaping ()->()) {
    onCompleted()
    }
如下调用函数

    let service = Service.shared
    service.doDownload { () in
        print("Called in completion Handler")
    }
有关闭包的更多详细信息,请访问以下链接。

您在viewDidLoad中的代码看起来不错,但是您是否可以重新访问singleton类,并在添加调用super.init的私有init方法后尝试运行代码,看看代码是否工作。

您在viewDidLoad中的代码看起来不错,但您是否可以重新访问singleton类,并在添加私有类后尝试运行代码init方法调用super.init并查看代码是否有效。

避免使用singleton,或删除属性
onCompleted
,并将其作为
doDownload
方法的参数。无论如何,您的
服务
类设计错误,需要更新。@OOPer我是noob swift开发者。你能给我写
删除属性
而不使用singleton
吗?谢谢,请先阅读,然后再回答你的问题,给出演示问题的最少代码。例如,我们不需要查看
removeMp3FileIfExists
的实现,避免使用singleton,或者删除属性
onCompleted
并将其作为
doDownload
方法的参数。无论如何,您的
服务
类设计错误,需要更新。@OOPer我是noob swift开发者。你能给我写
删除属性
而不使用singleton
吗?谢谢,请先阅读,然后再回答你的问题,给出演示问题的最少代码。例如,我们不需要看到
removeMp3FileIfExists
的实现谢谢您的回答
doDownload
是处理函数,不能在两次中运行它。我需要运行
doDownload
一次,并在两个视图(上下文)中获得两个
onCompleted
回调,然后如果您使用委托或通知会更好。我可以在多个视图中获得一个
协议委托
回调吗?谢谢您的回答
doDownload
是处理函数,不能在两次内运行它。我需要运行
doDownload
一次,并在两个视图(上下文)中获得两个
onCompleted
的回调那么如果您使用委托或通知会更好。我可以在多个视图中获得一个
协议委托
回调吗?您已经在singleton correct中添加了一个初始值设定项。能否从单例文件中删除let static shared,并在视图控制器中创建一个强引用,然后调用完成块,如let service=service();然后在viewDidLoad中调用service.onCompleted()和在collectionView中通过函数参数传递服务实例。闭包没有被调用的原因是我们没有很强的引用它。试试这个,让我知道你已经在singleton中添加了一个初始值设定项。能否从单例文件中删除let static shared,并在视图控制器中创建一个强引用,然后调用完成块,如let service=service();然后在viewDidLoad中调用service.onCompleted()和在collectionView中通过函数参数传递服务实例。闭包没有被调用的原因是我们没有很强的引用它。试试这个,让我知道