swift - Firebase PushViewController 多次推送

标签 swift firebase

在我的项目中,我删除了 Storyboard中的所有 Segues 并更改了所有

performSegue...

成为

pushViewController...

在我的应用程序中,75% 的时间都可以完美运行。然而,当我在 Firebase 方法中并尝试这样做时,我插入的 VC 在屏幕上闪烁了几次。

我认为 Firebase 与此有关,因为我创建了一个简单的方法

func testDel() {
    if let addVC = self.storyboard?.instantiateViewController(withIdentifier: "AddCardVC") as? AddCardViewController {
        self.navigationController?.pushViewController(addVC, animated: true)
        print("foo")
    }
}

它完全按照预期工作,去了第二个 VC,就是这样......

在发布代码之前,我为删除原来的问题表示歉意。我的编辑次数太多了。

到目前为止,我已经尝试将推送包装在

DispatchQueue.main.async {...}

这没有效果。

我转到第二个 VC 并注释掉所有函数,并在任何有 PushVC 的行上放置断点,只是为了看看它是否自动触发...

我在推送之后的方法中放置了一条打印语句,并且它打印一次。我还在第二个 VC ViewWillAppear 中放置了一条打印语句,该语句被调用 5 次。

我尝试注释掉我的推送代码,并在 Storyboard上重新添加一条连续线,并以老式的方式进行连续,它做了类似的事情,但是我在控制台中看到了一条日志,上面写着

unbalanced calls to begin/end appearance transitions for uiviewcontroller...

当我使用推送时不会出现

我还发现了一篇关于 SO 的旧帖子,其中有人建议了以下代码

var shouldPush = true

if let navigationController = self.navigationController {
for viewController in navigationController.viewControllers {
if viewController is AddCardViewController {
shouldPush = false
}
}
}

if shouldPush {
let addCrd = AddCardViewController(nibName: nil, bundle: nil)                       self.navigationController?.pushViewController(addCrd, animated: true)
}

这使得闪烁次数从大约 5 次减少到 2 次。仍然不是很好。

我的完整代码是这样的

// MARK: Delete Card with UIAlert

func deleteCard() {

    let alertController = UIAlertController(title: "Wait!", message: "This will completely remove this card from your account. All the services linked to this card will be removed. Your total fixed monthly expenses will also be erased!", preferredStyle: UIAlertControllerStyle.alert)

    let cancelAction = UIAlertAction(title: "Never Mind!", style: UIAlertActionStyle.cancel, handler: nil)

    let okAction = UIAlertAction(title: "I Understand!", style: UIAlertActionStyle.default) { (result: UIAlertAction) in

        let thisCard = self.ref.child("cards").child(self.thisCardIDTransfered)
        let thisCardInUsers = self.ref.child("users").child((self.user?.uid)!).child("cards").child(self.thisCardIDTransfered)

        thisCard.removeValue()
        thisCardInUsers.removeValue()

        let cardNode = self.ref.child("users").child((self.user?.uid)!).child("cards")

        cardNode.observeSingleEvent(of: .value, with: { (snapshot) in
            if snapshot.hasChildren() {

                if let walletVC = self.storyboard?.instantiateViewController(withIdentifier: "WalletVC") as? CardWalletViewController {
                    self.navigationController?.pushViewController(walletVC, animated: true)
                }

            } else {

                if let addVC = self.storyboard?.instantiateViewController(withIdentifier: "AddCardVC") as? AddCardViewController {
                    self.navigationController?.pushViewController(addVC, animated: true)
                    print("foo")
                }
            }
        })
    }

    alertController.addAction(cancelAction)
    alertController.addAction(okAction)

    self.present(alertController, animated: true, completion: nil)
}

最佳答案

cardNode.observeSingleEvent(of: .value, with: { (snapshot) in
            if snapshot.hasChildren() {
               ...

            } else {

            ...
        })

这个观察者可能会被多次调用,并使您的 View 被多次推送,也许检查快照包含的内容可以帮助您解决问题。我猜有时这个观察者会触发并且快照可能为空。

cardNode.observeSingleEvent(of: .value, with: { (snapshot) in
        if snapshot.value is NSNull {
            //do Nothing
        } else {
           //check if it contains children
        }

我不建议使用观察者来决定您应该显示什么 View 。特别是当您正在监听 .value 时,因为它们可能会意外触发,并且忘记删除观察者可能会导致许多错误。

关于swift - Firebase PushViewController 多次推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44478386/

相关文章:

ios - 使 setter 方法每 1 分钟递归一次

ios - Swift - UIButton 没有固定在屏幕底部

ios - 在自定义 tableview 单元格中本地化按钮和标签 [swift]

ios - UICollectionView : How do I implement auto-resizing of UICollectionViewCell in terms of its height?

ios - Swift Firebase 检查认证更顺畅

iOS 应用程序只能支持一个 tabBar 项目的横向方向吗?

javascript - Angularfire2,startAfter()不适用于分页

java - 未触发 Firebase 异步上传到 Cloud Firestore

android - 使用 Firebase 实时数据库时如何将用户从非授权转换为授权的最佳方法

node.js - 如何为 firebase-auth 提供 index.js 文件中服务 key 文件的路径?