firebase观察数据异步

发布于 2025-02-09 22:53:18 字数 1713 浏览 0 评论 0原文

我正在尝试以异步方式从实时数据库中观察数据。

为此,我使用“ with with CheckedContinature”返回结果,但是我在iOS文档中看到,延续只能是“简历”一次,因此每次我更新数据库中的数据时,都会破坏我的应用程序。

我想知道我正在尝试的方法是可能的,还是还有其他方法可以解决此问题,以使该功能保持异步。

class StoreFirebase {
    private let ref = Database.database().reference()
    var driverObserver: UInt?
    var driverReference: DatabaseReference?
    
    func getDriverSingleLocation(_ uuid: String) async -> Result<StoreData, Error> {
        
        return await withCheckedContinuation { continuation in
            driverReference?.observeSingleEvent(of: .value) { (snapshot) in
                if let dictionary = snapshot.value as? StoreData {
                    continuation.resume(returning: .success(dictionary))
                } else {
                    continuation.resume(returning: .failure(HTTPError(.badRequest)))
                }
            }
        }
    }
    
    func getDriverLocation(_ uuid: String) async -> Result<StoreData, Error> {
        driverReference = ref.child("driversAvailable").child(uuid)
        
        return await withCheckedContinuation { continuation in
            var nillableContinuation: CheckedContinuation<Result<StoreData, Error>, Never>? = continuation

            driverObserver = driverReference?.observe(.value) { (snapshot) in
                if let dictionary = snapshot.value as? StoreData {
                    nillableContinuation?.resume(returning: .success(dictionary))
                    
                    
                } else {
                    nillableContinuation?.resume(returning: .failure(HTTPError(.badRequest)))
                   
                }
            }
        }
    }
}

I am trying to observe data from the Realtime Database in an asynchronous way.

For this I use "withCheckedContinuation" to return the result but I have seen in the IOS documentation that Continuation can only be "resume" once so every time I update the data in the database this breaks my app.

I would like to know if what I am trying is possible or if there is some other way to deal with this problem so that the function remains asynchronous.

class StoreFirebase {
    private let ref = Database.database().reference()
    var driverObserver: UInt?
    var driverReference: DatabaseReference?
    
    func getDriverSingleLocation(_ uuid: String) async -> Result<StoreData, Error> {
        
        return await withCheckedContinuation { continuation in
            driverReference?.observeSingleEvent(of: .value) { (snapshot) in
                if let dictionary = snapshot.value as? StoreData {
                    continuation.resume(returning: .success(dictionary))
                } else {
                    continuation.resume(returning: .failure(HTTPError(.badRequest)))
                }
            }
        }
    }
    
    func getDriverLocation(_ uuid: String) async -> Result<StoreData, Error> {
        driverReference = ref.child("driversAvailable").child(uuid)
        
        return await withCheckedContinuation { continuation in
            var nillableContinuation: CheckedContinuation<Result<StoreData, Error>, Never>? = continuation

            driverObserver = driverReference?.observe(.value) { (snapshot) in
                if let dictionary = snapshot.value as? StoreData {
                    nillableContinuation?.resume(returning: .success(dictionary))
                    
                    
                } else {
                    nillableContinuation?.resume(returning: .failure(HTTPError(.badRequest)))
                   
                }
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷不放纵 2025-02-16 22:53:18

thx发表评论,用异步链修复

class RealTimeDatabaseService: RealTimeDatabaseProtocol {
    private let ref = Database.database().reference()
    var driverObserver: UInt?
    var driverReference: DatabaseReference?
    
    func getDriverLocation(_ uuid: String) -> AsyncStream <Result<StoreData, Error>> {
        driverReference = ref.child("driversAvailable").child(uuid)
        
        return AsyncStream { continuation in
            driverObserver = driverReference?.observe(.value) { (snapshot) in
                if let dictionary = snapshot.value as? StoreData {
                    continuation.yield(.success(dictionary))
                } else {
                    continuation.yield(.failure(HTTPError(.badRequest)))
                    continuation.finish()
                }
            }
        }
    }

    func getDriverLocationResult() async  {
        for try await response in 'getDriverLocation'("uid") {
            switch response {
            case .success(let storeDate):
                print("completed: \(storeDate)")
            case .failure(let error):
                print("error: \(error)")
            }
        }
    }

Thx for the comment, fixed it with AsyncStream

class RealTimeDatabaseService: RealTimeDatabaseProtocol {
    private let ref = Database.database().reference()
    var driverObserver: UInt?
    var driverReference: DatabaseReference?
    
    func getDriverLocation(_ uuid: String) -> AsyncStream <Result<StoreData, Error>> {
        driverReference = ref.child("driversAvailable").child(uuid)
        
        return AsyncStream { continuation in
            driverObserver = driverReference?.observe(.value) { (snapshot) in
                if let dictionary = snapshot.value as? StoreData {
                    continuation.yield(.success(dictionary))
                } else {
                    continuation.yield(.failure(HTTPError(.badRequest)))
                    continuation.finish()
                }
            }
        }
    }

    func getDriverLocationResult() async  {
        for try await response in 'getDriverLocation'("uid") {
            switch response {
            case .success(let storeDate):
                print("completed: \(storeDate)")
            case .failure(let error):
                print("error: \(error)")
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文