【问题标题】:Firebase Swift: queryEqualToValue by childKey is not workingFirebase Swift:childKey 的 queryEqualToValue 不起作用
【发布时间】:2016-10-01 07:43:32
【问题描述】:

有没有人在使用该功能时运气好:

.queryEqualToValue(value: AnyObject?, childKey: String?)

两件事:

1) childKey 似乎不允许深度路径,只允许直接子级

2) 我根本无法让它工作!鉴于我的结构:

"post": {
  "groupName": "hello world"
}

如果我做一个简单的:

postRef.queryEqualToValue("hello world", childKey: "groupName").observeSingleEvent( ... )

它根本不返回任何帖子。相反,我必须采取迂回的方式:

postRef.queryOrderedByChild("groupName").queryEqualToValue("hello world").observeSingleEvent( ... )

让它工作!

我是不是用错了上面的功能?

【问题讨论】:

  • 最后一个 sn-p 看起来是正确的。第一个sn-p总是给人带来麻烦,所以我倾向于忽略它。
  • 我无法让queryOrderedByChild("").queryEqualToValue("") 工作!?

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

Xcode 7.2.1
斯威夫特 2.1.1
iOS 9.2
OSX 10.10.5

2) 我根本无法让它工作!鉴于我的结构:

"post": {
  "groupName": "hello world"
}

如果我做一个简单的:

postRef.queryEqualToValue("hello world", childKey: "groupName")
postRef.queryEqualToValue("hello world", childKey: "groupName").observeSingleEvent( ... )

我可以通过以下代码成功获取查询:

import UIKit
import Firebase

class ViewController: UIViewController {

    var dbRef: FIRDatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        dbRef = FIRDatabase.database().reference()
        dbRef.child("post").child("groupName").setValue("hello world")

        let postRef = dbRef.child("post")
        print(postRef)

        let query = postRef.queryEqualToValue("hello world", childKey: "groupName")
        print(query)


    --output:--
    (/post {
        en = groupName;
        ep = "hello world";
        sn = groupName;
        sp = "hello world";
    })

1) childKey 似乎不允许深度路径,只是直接 孩子们

我没看到。如果我将代码更改为:

import UIKit
import Firebase

class ViewController: UIViewController {

    var dbRef: FIRDatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        dbRef = FIRDatabase.database().reference()
        dbRef.child("post").child("groupName").child("userId").setValue("hello world")

        let postRef = dbRef.child("post")
        print(postRef)

        let query = postRef.queryEqualToValue("hello world", childKey: "userId")
        print(query)

我得到以下输出:

(/post {
    en = userId;
    ep = "hello world";
    sn = userId;
    sp = "hello world";
})

但是,我无法让观察者工作——他们肯定不会像文档所说的那样工作。

我在运行应用程序之前删除了我在 Firebase 上的所有数据。

编辑:好的,我想我知道我的观察者在做什么。根据Retrieve Data 部分:

通过将异步侦听器附加到 FIRDatabase 参考 FIRDatabaseReference。侦听器在初始时触发一次 数据的状态,并在数据更改的任何时候再次出现。

当我尝试观察查询时,回调中的快照中没有任何数据。另一方面,当我观察到FirDatabaseReference 时,观察者的工作(有点)像我预期的那样。

例如,如果我观察到这样的 FIRDatabaseReference:

import UIKit
import Firebase

class ViewController: UIViewController {

    var dbRef: FIRDatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        dbRef = FIRDatabase.database().reference()
        dbRef.child("post").child("groupName").child("userId").setValue("hello world")

        let postRef = dbRef.child("post")
        postRef.observeEventType(.Value, withBlock: { (snapshot) -> Void in
            print(snapshot)
        })

        sleep(1) //Allow the observer (in another thread) to execute before the following line:
        dbRef.child("post").child("groupName").child("userId").setValue("goodbye Mars")


    }

然后回调中的快照中有数据,我得到这个输出:

Snap (post) {
    groupName =     {
        userId = "hello world";
    };
}
Snap (post) {
    groupName =     {
        userId = "goodbye Mars";
    };
}

但如果我观察到 FIRDatabaseQuery:

let postRef = dbRef.child("post")
let query = postRef.queryEqualToValue("hello world", childKey: "userID")

query.observeEventType(.Value, withBlock: { (snapshot) -> Void in
    print(snapshot)
})

那么输出是:

Snap (post) <null>

【讨论】:

    【解决方案2】:

    Firebase 文档中编写了以下内容:

    queryEqualToValue 根据选择的排序方法返回等于指定键、值或优先级的项目。

    Retrieve Data on iOS

    重要的部分是:“取决于选择的排序方法。”

    因此,请确保将 queryEqualToValue-Method 与 orderBy-Method 结合使用。

    例如:

        let groupState = "open"
    
        ref.child("groups").queryOrdered(byChild:"state").queryEqual(toValue:groupState).observe(enventType: .value, with: { snapshot in
            // Returns all groups with state "open"
            for group in snapshot.children {
                print(group)
            }
        })
    

    PS:新的 Firebase iOS 指南中没有提到带有 childKey 的变体。

    【讨论】:

    • childKey 变体是否已弃用?好像不行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 2018-09-18
    • 2018-06-26
    • 2018-02-07
    • 2016-12-05
    • 2018-10-22
    相关资源
    最近更新 更多