在Firebase SWIFT中查询数组 [英] Query an array in Firebase SWIFT

查看:53
本文介绍了在Firebase SWIFT中查询数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用swift和firebase,我有一个像这样的数据列表

using swift and firebase I have a list of data like so

{
"posts" : {
 "-KHbULJcKqt9bSpxNO9k" : {
  "author" : "Michele",
  "postText" : "tags as arrays?",
  "tag" : [ "HELLO", "WHAT'S UP?", "TEST" ]
}

还有更多帖子。我想按标签搜索,如果某个帖子包含作为标签的搜索词,则返回整个帖子。我已经能够使用 queryEqualToValue:使用字符串来完成此操作,但无法为数组求解。

with many more post. I want to search by tag and if a post contains the search term as a tag return the whole post. Ive been able to do this with strings using queryEqualToValue: but haven't been able to solve it for an array. Any point in the right direction is appreciated thank you.

这就是我保存功能的样子

This is what my save function looks like

func createNewPost(post: Dictionary<String, AnyObject>, tags: [String]) {
    let firebaseNewPost = POST_REF.childByAutoId()
    let firebaseTag = Firebase(url: "\(POST_REF)/\(firebaseNewPost.key)/tag")

    print(firebaseTag)


    firebaseNewPost.setValue(post)

    for tag in tags {
        let tagID = firebaseTag.childByAutoId()

        tagID.setValue(tag)
    }
}

现在我需要搜索数据并根据用户搜索的内容检索某些帖子的帮助。这就是我在使用的只是一个标签而只是一个字符串的情况。

Now I need help with searching through the data and retrieving certain post based on what a user searches for. This is what I was using when it was just one tag and it was a just a string.

    func loadTaggedShit(searchTerm: String) {

    DataService.dataService.POST_REF.queryOrderedByChild("tag").queryEqualToValue(seachTerm).observeEventType(.ChildAdded, withBlock: { snapshot in
        self.posts = []

        if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {
            for snap in snapshots {
                if let postDictionary = snap.value as? Dictionary<String, AnyObject> {
                    let key = snap.key
                    let post = Post(key: key, dictionary: postDictionary)

                    self.posts.insert(post, atIndex: 0)
                }
            }
        }

        self.tableView.reloadData()


    })

}

现在数据也看起来像这样

also the data now looks like this

"posts":{
 "-KHj_bDJmZJut7knKoUX" : {
  "author" : "Michele",
  "postText" : "what's up",
  "tag" : {
    "-KHj_bDJmZJut7knKoUY" : "HEY"
   }
 }
}


推荐答案

Firebase数组非常特殊,应尽可能避免。

Firebase arrays are very situational and really should be avoided if possible.

数组中的代码是非常强大的数据构造,但它们只是在JSON结构中不能很好地工作。如果修改了它们,例如删除了一个节点,那么该索引也将被删除。这样会使阵列上留有空洞,例如0、1、2、4、5、7等。因此,请在您的post节点中创建一个标签子节点,然后使用childByAutoId

Array's in code are very powerful data constructs but they just don't work well within a JSON structure. If they are modified, say, removing a node, that index will be removed as well. That leaves the array with 'holes' e.g. 0, 1, 2, 4, 5, 7 etc. So, create yourself a tags child node within your post node, and then child nodes with keys created with childByAutoId

"posts" : {
 "-KHbULJcKqt9bSpxNO9k" : {
    "author" : "Michele",
    "postText" : "tags as arrays?",
    "tags"
       tag_id_0
         tag_title: "HELLO"
       tag_id_1
         tag_title: "WHAT'S UP?"
       tag_id_2
         tag_title: "TEST"
}

甚至考虑创建一个单独的标签节点并在posts节点中引用它-尤其是如果它们将可重用。

You might even consider creating a separate tags node and reference it within the posts node - especially if they are going to be re-usable.

编辑:

基于一些其他信息,将形成不同的结构,因为OP需要查询包含特定标签的帖子,而这些标签实际上是可重用的,而不是特定于帖子的:

Based on some additional information, a different structure will be in order because the OP needs to query for posts that contain a specific tag and the tags are in fact reusable and not specific to a post:

posts
  -KHbULJcKqt9bSpxNO9k
    author: "Michele"
    postText: "tags as arrays?"
    tags
      tag_id_1: true
      tag_id_2: true
  -JPHJSojo91920LLK0J
    author: "Larry"
    postText: "NO, don't use arrays"
    tags
     tag_id_2: true

tags
  tag_id_0
    tag_title: "HELLO"
  tag_id_1
    tag_title: "WHAT'S UP?"
  tag_id_2
    tag_title: "TEST"

然后接收所有代码使用TEST标签tag_id_2

And then the code to receive all posts that use the TEST tag, tag_id_2

这称为Firebase 深入查询

This is called a Firebase Deep Query

postsRef.queryOrderedByChild("tags/tag_id_2").queryEqualToValue(true)
        .observeSingleEventOfType(.Value, withBlock: { snapshot in
    print(snapshot)
})

这篇关于在Firebase SWIFT中查询数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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