ios - 如何从 Firebase Cloud Firestore 提取数据

标签 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

最佳答案

从 Firebase 获取数据然后使用它

步骤:-1获取数据

    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)
       
    })

步骤:- 2从快照中提取数据

var arrFetchedData:NSMutableArray = NSMutableArray()
    
    for data in dictData{
        print(data.key) //Key will be printed here
        
        var tempDict:NSMutableDictionary = NSMutableDictionary()
        
        let innerData = data.value as! [String:Any]
        
        let expires = innerData["expires"]
        tempDict.setValue(expires, forKey: "expires")
        let name = innerData["name"]
        tempDict.setValue(name, forKey: "name")
        let url = innerData["url"]
        tempDict.setValue(url, forKey: "url")
        arrFetchedData.add(tempDict)
    }
    
    print(arrFetchedData) //All the Fetched data will be here

希望这有帮助!

关于ios - 如何从 Firebase Cloud Firestore 提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49720335/

相关文章:

ios - 如何在委托(delegate)函数中关闭 alertview - (void)alertView :(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

swift - http header 中的 Yelp 身份验证

ios - 是否可以使用 NavigationBarViewController 作为 UINavigationController 将 View Controller 推送到堆栈上

android - 限制 Firebase 数据库访问一个 Android 应用程序

ios - 无法在我的 iOS 设备上播放 firebase 视频

ios - 设置表格 View 单元格的最小高度?

ios - 这是什么意思? "alienPosition = alien.position"

swift - 相对于不同节点的不同 zPositions

node.js - 错误 : read ECONNRESET when resizing image with Firebase Cloud Functions

javascript - 如何查询和过滤 Firebase 实时数据库