Firebase错误地显示删除的数据/ Swift 3 / Xcode 8.2 [英] Firebase erroneously shows deleted data / Swift 3 / Xcode 8.2

查看:169
本文介绍了Firebase错误地显示删除的数据/ Swift 3 / Xcode 8.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,刚开始使用Firebase,我正在运行下面的函数,基本上试图创建2个节点,锦标赛和每日游戏(如果它们不存在的话)。



数据库树应如下所示:

  -London 
- 游戏
- 日期(stringDate)//从另一个函数中获取这个函数,返回2016-16-12
-Tournaments
--Daily Games


$ b

代码如下:

pre $ $ $ c> func createTournamentsAndNonTournamentsNodes(){

let databaseRef = FIRDatabase.database()。reference()
self.getLiveTimestamp()//调用一个返回时间戳的函数


databaseRef.child(London / City Game / \(stringDate))。observeSingleEvent(of:.value,with:{(snapshot)in

if snapshot。 hasChild(Daily Games){
print(Daily Games node already exists)
print(snapshot.key)
print(snapshot.value as Any)
} else {
print(每日游戏不存在,现在创建 )
让postTwo:[String:AnyObject] = [(每日游戏):今天在:\(self.timeStampString)发起的任何对象]

databaseRef.child London / City Game / \(stringDate))。child(Daily Games)。updateChildValues(postTwo)
}
如果snapshot.hasChild(Tournaments){
print (锦标赛节点已经存在)
print(snapshot.key)
print(snapshot.value as Any)
} else {
print(Tournaments dont exist,creating it现在)
让postOne:[String:AnyObject] = [(Tournaments):作为AnyObject在今天启动:\(self.timeStampString)
databaseRef.child(London / City Game / \(stringDate))。child(Tournaments)。updateChildValues(postOne)
}
})
}

所以在空数据库i上运行上面的结果控制台打印出每日游戏节点已经存在和锦标赛节点已经存在,其中键和值如下。但问题在于,这些是我在1小时前通过以前的运行创建的,并立即从Firebase Web控制台中手动删除,以便测试代码,在再次运行代码之前将数据库完全清空。因此,重新加载Firebase控制台会向我显示一个空的数据库,但是我的代码打印出来,就好像数据库中存在数据一样。这怎么可能? Firebase是否保留了一段时间的数据,尽管在Web控制台上显示为空?

 可选({

每日游戏=今天开始:13:01:19;
Tournaments = {
Tournaments =今天发起于:13:05:22;
};
$)

我做错了什么?如果我的代码向我展示了一个填充数据库,但是我的Web控制台却显示为空的数据库,如何真正知道数据库中的内容是什么?非常感谢

更新:
$ b 我有FIRDatabase.database()。persistenceEnabled = true,并在真实设备上运行。禁用persistenceEnabled后,它工作得很好。有什么办法解决这个问题,而不禁用persistenceEnabled?

解决方案

好的,这里是我的提示。同样的问题,并解决这个我从 observeSingleEvent 更改为观察,然后Firebase不仅看着你的手机,因为这样做当你这样做。


I am new to coding and just started using Firebase, and I am running the below function which is basically trying to create 2 nodes, "Tournaments" and "Daily Games" (if they don't exist already).

The database tree should look like this:

-London
  -City Game
      -Date(stringDate)  // getting this from another function, returns 2016-16-12
           -Tournaments
           -Daily Games

And the code looks like this:

     func createTournamentsAndNonTournamentsNodes() {

        let databaseRef = FIRDatabase.database().reference()
        self.getLiveTimestamp()  // this calls a function that returns timestamp


        databaseRef.child("London/City Game/\(stringDate)").observeSingleEvent(of: .value, with:  { (snapshot) in

            if snapshot.hasChild("Daily Games") {
                print ("Daily Games node exists already")
            print(snapshot.key)
            print(snapshot.value as Any)
            } else {
                print ("Daily Games dont exist, creating it now")
                let postTwo : [String : AnyObject] = [("Daily Games") : "Initiated today at : \(self.timeStampString)" as AnyObject]

                databaseRef.child("London/City Game/\(stringDate)").child("Daily Games").updateChildValues(postTwo)
            }
            if snapshot.hasChild("Tournaments") {
                print ("Tournaments node exists already")
            print(snapshot.key)
            print(snapshot.value as Any)
            } else {
                print ("Tournaments dont exist, creating it now")
                let postOne : [String : AnyObject] = [("Tournaments") : "Initiated today at : \(self.timeStampString)" as AnyObject]
                databaseRef.child("London/City Game/\(stringDate)").child("Tournaments").updateChildValues(postOne)
            }
        })
    }

So the result of running the above on an empty database is that the console prints "Daily game node exists already" and "Tournaments node exists already", with key and values as below. But the problem is that those were created by me on a previous run 1 hr ago and immediately deleted manually from the Firebase web console, to test the code, making my database completely empty before running the code again. So reloading the Firebase console shows me an empty database, but my code prints me this, as if the data exists in the database. How is this possible? Does Firebase keep data for some time despite appearing empty on the web console?

Optional({

    "Daily Games" = "Initiated today at : 13:01:19";
    Tournaments =     {
        Tournaments = "Initiated today at : 13:05:22";
    };
})

What am I doing wrong? How to really tell what is and what is not in the database if my code shows me a populated database, but my Web Console shows me an empty one? Many thanks

UPDATE:

I had FIRDatabase.database().persistenceEnabled = true and also running this on a real device. After disabling persistenceEnabled, it works absolutely fine. What is the way to fix this, without disabling persistenceEnabled?

解决方案

Well, here is my tip.

I had almost the same problem, and to solve this I changed from observeSingleEvent to observe and then Firebase does not only look into your cellphone, because that's exactly what it does when you do it.

这篇关于Firebase错误地显示删除的数据/ Swift 3 / Xcode 8.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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