Firebase

时间:2017-02-10 16:52:02

标签: ios swift firebase firebase-realtime-database

如何访问“更深层次”的信息?快照的级别?我是否总是需要启动另一个查询?为了告诉你我的意思,我有一个结构截图: Screenshot of DataStructure

我正在加载查询中的所有帖子,我可以阅读作者信息等,但我如何获取选项内的信息?我尝试了以下方法:

ref.child("posts").child("details").observe(.childAdded, with: { (snapshot:FIRDataSnapshot) in

        if snapshot.value == nil {
            return
        }
        let post = Post()
        guard let snapshotValue = snapshot.value as? NSDictionary else {
            return
        }
        post.postID = snapshot.key
        post.title = snapshotValue["title"] as? String
        post.postDescription = snapshotValue["description"] as? String
        if (post.postDescription == ""){
            post.postDescription = "No description has been entered for this post.."
        }
        post.timestamp = snapshotValue["timestamp"] as? NSNumber

        let options = snapshotValue["options"]
        print(options)

当我打印选项时,我可以看到这些信息,但是当我尝试将其转换为NSDictionary或者某些东西来访问它时,它打印为nil?我还可以定义一个像post.options这样的新属性,如果这可能有帮助吗?选项并不总是0和1,它的变量,所以我需要迭代这些。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

跟进对原始问题的评论,您可以访问以下选项值:

const describe = require('ava-spec').describe;
const request = require('supertest-as-promised');
const server = require('../../../../app');

describe('controllers', () => {
    describe('import_file', () => {
        describe('POST /importfile', (it) => {
            it('should accept a file parameter', async (t) => {
                const res = await request(server)
                    .post('/importfile')
                    .attach('file', 'test/data/A.csv' )
                    .expect(200);
                t.is(res.body, 'file uploaded');
            });
        });
    });
});

答案 1 :(得分:0)

这是一个非常简化的答案,适用于所提供的Firebase结构(请注意,Firebase数组只应在特定情况下使用,通常应避免使用)。

给定结构

posts
  post_id_0
    caption: "some caption"
    comment: "some comment"
    options:
       0:
          votes: "10"
          url: "some url"
       1:
          votes: "20"
          url: "some url"

请注意,该结构与OP类似,因为options子节点是Firebase数组,其中每个索引还包含子节点。所以关键是索引(0,1,2),每个的子节点都是键的字典:值对(votes:value和url:value)

以下代码将在此特定节点中读取,打印标题和注释,然后迭代选项ARRAY并打印数组中每个子项的投票和URL

ref.child("posts").child("post_id_0")
                  .observeSingleEvent(of: .value, with: { snapshot in
     //treat the whole node as a dictionary
     let postDict = snapshot.value as! [String: Any]

     //and since it's a dictionary, access each child node as such       
     let caption = postDict["caption"] as! String
     let comment = postDict["comment"] as! String

     print("caption: \(caption)  comment: \(comment)")

     //the options child node is an array of indexes, each with
     //   a dictionary of child nodes     
     let options = postDict["options"] as! [[String:Any]] //array of dictionaries

     //now iterate over the array      
     for option in options {
          let votes = option["votes"] //each option child is a dict
          let url = option["url"]
          print("votes \(votes!)  url: \(url!)")
     }
})

请注意,我们像这样指示选项子节点的结构

let options = postDict["options"] as! [[String:Any]]

这是一个key:value(String:Any)字典

的数组

非常重要的一点是必须读入整个选项节点才能使用它。如果有10个孩子,那就变得非常低效。它也是不可查询的,所以如果你想计算有多少重复的url,那么必须读入整个节点然后进行解析。

更好的选择是使用childByAutoId创建节点密钥名称

posts
  post_id_0
    caption: "some caption"
    comment: "some comment"
    options:
       -Yuijjismisdw
          votes: "10"
          url: "some url"
       -YJ989jijdmfm
          votes: "20"
          url: "some url"

Yuijjismisdw是用childByAutoId创建的。