【问题标题】:Receiving Firebase autoId in tableViewController在 tableViewController 中接收 Firebase autoId
【发布时间】:2016-09-17 10:53:03
【问题描述】:

我有一个看起来像这样的firebase 结构:

蓝色的是 feedPosts,绿色的是 userId,没有颜色的是 firebase 为附加数据创建的 autoId,橙色的是 content: "users post string is here" .

我的问题是我不知道如何获取firebase autoId,以便我可以显示用户在我的tableViewController 中发布的内容。我需要将数据附加到 userId 以便用户可以发布多个帖子 - 我只是不知道如何获取 tableViewController 中的密钥。帮帮我?

【问题讨论】:

  • 你事先知道那个 autoID 里有什么吗?否则检索整个数据、循环遍历每个 autoID 并检索单个数据节点将是愚蠢的。
  • 不,我事先不知道 autoID 是什么。你能给我举个例子吗?

标签: ios swift uitableview firebase firebase-realtime-database


【解决方案1】:

最简单的解决方案是更改结构以匹配您尝试获取的数据。这就是你现在拥有的

feedPosts
   -fbUserId0
     -autoIdForThisPost1
        content: "users post string is here"
   -fbUserId1
     -autoIdForThisPost1
        content: "users post string is here"

改成这个

feedPosts
   -autoIdForThisPost0
      post_by_uid: "-fbUserId0"
      content: "users post string is here"
   -autoIdForThisPost1
      post_by_uid: "-fbUserId1"
      content: "users post string is here"

然后您可以通过-fbUserId1 查询所有帖子。这将返回每个帖子节点,其中包含内容以及作为 autoId 的帖子的 KEY。

这也是可扩展的,因为您还可以存储例如关于帖子的时间戳,或者甚至可能是响应的帖子。

另一个优点是使整个结构更扁平(更扁平更好!在 Firebase 中非常重要)并且更易于查询。

【讨论】:

  • 我会尝试这样做 - 这听起来是最好的解决方案!如何查询 userId?
  • @D.Finna 在此处查看指南Retrieving Data,并在过滤数据部分查看 queryEqualToValue。
  • 谢谢!这听起来像是我可以使用的东西 :-) 我一定会看看的!
【解决方案2】:

如果你的 JSON 是这样的:-

foodItems{
      uid1 :{
          autoID1 :{
                content : "This is a String"
         }
      }
    }

检索包含 content : "This is a String"

的 autoID

斯威夫特 2.3

   let parentRef = FIRDatabase.database().reference().child("foodItems/\(FIRAuth().auth()!.currentUser!.uid)").queryOrderedByChild("content").queryEqualToValue("This is a String")

     parentRef.observeSingleEventOfType(.Value, withBlock:{(snap) in 

      if let snapContainingContent = snap.value as? [String:AnyObject]{

         for each in snapContainingContent{
             print(each.0)//your autoID
             print(each.1)//your content String
             if let autoDict = each.1 as? NSDictionary{

                   print(autoDict["content"]!)
                   print(autoDict.allKeys(for: "This is a String")[0])


                }
            }

       }

 })

斯威夫特 3

let parentRef = FIRDatabase.database().reference().child("Posts/foodItems/uid1").queryOrdered(byChild: "content").queryEqual(toValue: "This is a String")

    parentRef.observeSingleEvent(of: .value, with:{(snap) in

        if let snapContainingContent = snap.value as? [String:AnyObject]{

            for each in snapContainingContent{
                print(each.0)//your autoID
                print(each.1)//your content String
                if let autoDict = each.1 as? NSDictionary{

                   print(autoDict["content"]!)
                   print(autoDict.allKeys(for: "This is a String")[0])
                }

            }

        }

    })

【讨论】:

  • 但这只会显示代码的特定部分?我想显示所有内容:“带字符串” - 这不可能吗?
  • 我已经编辑了我的答案...这是您使用 autoID 可以获得的最佳效果。另一种方法是遍历整个节点以在该子节点中搜索特定内容:)快乐编码
猜你喜欢
  • 2017-04-04
  • 2016-11-09
  • 1970-01-01
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多