如何从Firebase Cloud Firestore中提取数据

时间:2018-04-08 16:54:00

标签: ios swift firebase google-cloud-firestore

我试图从Firebase Firestore中提取所有对象(参见下图)。如何将输出结果添加到字典中?

func getLinks() {
    let user = Auth.auth().currentUser
    let userID = user?.uid
    let db = Firestore.firestore()
    print(userID!)
    let docRef = db.collection("files").document(userID!)

    docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
            print("Document data: \(dataDescription)")
        } else {
            print("Document does not exist")
        }
    }

}

Output Image

2 个答案:

答案 0 :(得分:0)

从Firebase获取数据然后使用

步骤:-1 获取数据

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>


using namespace rapidjson;


//({ id: 1, name : "test", randomNo : 1 }, { id: 1, name : "test", randomNo : 1 }, { id: null, name : null, randomNo : null }) //shoult assert true 
//isEqualItem({id: 1, name: "test", randomNo: 1}, {id: 1, name: "test", randomNo: 2}, {id: null, name: null, randomNo: null}) //shoult assert false 
//isEqualItem({id: 1, name: "test", randomNo: 1}, {id: 1, name: "test", randomNo: 3}, {id: null, name: null}) //shoult assert true

bool is_same(const std::string& s1, const std::string& s2, const std::string& s3) {

    Document d1;
    d1.Parse(s1.c_str());
    Document d2;
    d2.Parse(s2.c_str());

    Document d3;
    d3.Parse(s3.c_str());

    bool matched = true;
    // iterate through the third json to get the keys and match the keys in first and second
    for (Value::ConstMemberIterator itr = d3.MemberBegin(); itr != d3.MemberEnd(); itr++) {
        if (d1.HasMember(itr->name) && d2.HasMember(itr->name)) {       // if the member doesn't exist in both, break
            if (d1[itr->name] != d2[itr -> name]) {                     // value doesn't match, then break
                matched = false;
                break;
            }
        }
        else {
            matched = false;
            break;
        }
    }
    return matched;
}

int main() {
    // 1. Parse a JSON string into DOM.
    const char* json = "{\"id\":1,\"name\":\"test\",\"randomNo\":1}";
    const char* json2 = "{\"id\":1,\"name\":\"test\",\"randomNo\":2}";
    const char* keys = "{\"id\":\"null\",\"name\":\"null\"}";


    if (is_same(json, json2,keys)) {
        std::cout << "Both are same" << std::endl;
    }
    return 0;
}

步骤: - 2 从SnapShot中提取数据

    var dictData = [String:Any]() 
    let ref = Database.database().reference()
    ref.observe(.childAdded, with: { (snapshot) in
        print(snapshot.value!)
        self.dictData = snapshot.value as! [String : Any]
        print(self.dictData)

    })

希望这有帮助!

答案 1 :(得分:0)

您只需删除将字典转换为String的代码。

func getLinks() {
    let user = Auth.auth().currentUser
    let userID = user?.uid
    let db = Firestore.firestore()
    print(userID!)
    let docRef = db.collection("files").document(userID!)

    docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            let dataDescription = document.data()
            print("Document data:", dataDescription)
        } else {
            print("Document does not exist")
        }
    }

}