Swift ios Firebase不返回任何数据 [英] Swift ios Firebase not returning any data

查看:166
本文介绍了Swift ios Firebase不返回任何数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  handle = ref?.child(Users)。 child(String(itemId))。observe(.childChanged,with:{(snapShot)in 

如果让dictionary = snapShot.value as?[String:Any] {
print(dictionary )
如果让profileImageUrl = dictionary [url] as?String {
print(profileImageUrl)
}
}
},withCancel:nil)

我启动我的应用程序,然后进入我的Firebase控制台并对该子进行一些更改,但是我的 print()永远不会被解雇。



这就是我的数据库的样子:

  MyRootDb123 
- 用户
- 用户的子元素
- 内部元素Id
- 名称里面Id
---年龄里面Id

我的代码, withCancel 函数做了什么?

数据库结构:



更新
我加了 print(snapShot)

$ p $ Snap(url)www.someurl.com
Firebase对节点键名是区分大小写的,所以 不同于 用户,您的代码正在访问用户,但是结构是Users。



code

  handle = ref?.child(users)

结构

  MyRootDb123 
- 用户
- )

更新显示如何将.childChanged观察者附加到用户节点并处理更改为au ser。



让我们从一个基本的Firebase结构开始吧!
$ b $ pre $ users
uid_0
名字:Leonard
url:www.leonard.com
uid_1
名字:Bill
url:www.bill。 com

然后我们附加一个.childChanged观察者到用户节点。当用户节点中的子节点发生变化时,更改的节点将传递给闭包。

(users)

usersRef.observe(.childChanged,其中:{snapshot in
let userDict = snapshot.value as![String:AnyObject]
let name = userDict [name] as!String
let url = userDict [url] as!String
print(\(name)now has url:\(url))




$ p要测试这个,在Firebase控制台中,我们将uid_0的url子项从www .leonard.com到www.yipee.com,像这样

 用户
uid_0
名称: Leonard
url:www.yipee.com//这是新的网址
uid_1
名称:Bill
url:www.bill.com

由于更改位于uid_0中,所以将该节点传递给上面代码中的闭包,并将打印: / p>

  Leonard现在的网址是:www.yipee.com 
B $ b

I am trying to fetch values live using this function:

handle = ref?.child("Users").child(String(itemId)).observe(.childChanged, with: { (snapShot) in

                        if let dictionary = snapShot.value as? [String: Any] {
                            print(dictionary)
                            if let profileImageUrl = dictionary["url"] as? String {
                                print(profileImageUrl)
                            }
                        }
                    }, withCancel: nil)

I start my app then I go to my firebase console and make some changes to that child but my print() never gets fired.

This is how my DB looks like:

MyRootDb123
-Users
   --Id (Child of users)
     --- url inside Id
     --- name inside Id
     --- age inside Id

Also in my code, what does the withCancel function do?

DB structure:

Update I added print(snapShot) which returns:

Snap (url) www.someurl.com

解决方案

Firebase is case sensitive for node key names so Users is not the same as users and your code is accessing users but the structure is Users.

code

handle = ref?.child("users")

structure

MyRootDb123
-Users
   --Id (Child of users)

Update to show how to attach an .childChanged observer to the users node and handle a change to a user.

Let's start with a basic Firebase structure

users
  uid_0
    name: "Leonard"
    url: "www.leonard.com
  uid_1
    name: "Bill"
    url: "www.bill.com"

then we attach a .childChanged observer to the users node. When a child in the users node changes, the changed node is passed to the closure.

let usersRef = self.ref.child("users")

usersRef.observe(.childChanged, with: { snapshot in
    let userDict = snapshot.value as! [String: AnyObject]
    let name = userDict["name"] as! String
    let url = userDict["url"] as! String
    print("\(name) now has url of:  \(url)")
})

To test this, in the Firebase console we change the url child of uid_0 from www.leonard.com to www.yipee.com, like this

users
  uid_0
    name: "Leonard"
    url: "www.yipee.com" //here's the new url
  uid_1
    name: "Bill"
    url: "www.bill.com"

Since the change was in uid_0, that node it passed to the closure in the code above and will print:

Leonard now has url of:  www.yipee.com

这篇关于Swift ios Firebase不返回任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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