嵌套Firebase完成块 [英] Nested Firebase Completion Blocks

查看:72
本文介绍了嵌套Firebase完成块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在开发一个需要以下代码的应用程序:

Recently I have been developing an app which requires the following code:

/** Unfriends the user with the specified UID */
func removeFriend(_ userID: String, completion: CompletionHandler? = nil) {
    CURRENT_USER_FRIENDS_REF.document(userID).delete { (error) in
        guard error == nil else{
            completion?(error)
            return
        }

        self.users.document(userID).collection(NameFile.Firebase.UserDB.friends).document(AppStorage.PersonalInfo.uid).delete(completion: completion)
    }
}

在嵌套这些块时出现问题.如果第一个块成功,但是第二个块引发错误,则将向完成处理程序传递错误.但是,实际上,一半的过程成功完成并成功写入数据库.是否可以将这两个块作为一个块一起工作,如果发生错误,则该块会通过一个错误. (无需重组数据库)

The problem arises in nesting these blocks. If the first blocks succeeds, but the second block throws an error, the completion handler will be passed an error. However, in reality, half the process succeeded and wrote successfully to database. Is it possible to have both these blocks work together as one block which passes an error if an error occurs. (without restructuring the database)

推荐答案

如果您有多个写入操作必须调用成功或全部失败,则应使用

If you have multiple write operations that must either call succeed or all fail, you should use a transaction or batched write. The difference between the two is whether you need the current value of a document to determine its new value. If you don't need the current value for any document, use a batched write. If you do need the current value for a document, use a transaction for all writes.

这篇关于嵌套Firebase完成块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆