简体   繁体   English

使用Swift在Firebase中读取快照中的数组

[英]Read array inside snapshot in Firebase with Swift

Need help trying to read an array of this form: 需要帮助来尝试读取这种形式的数组:

Firebase数据库

As far as I tried, I got this 据我尝试,我得到了

let defaults = UserDefaults.standard
let userUuid = defaults.string(forKey: defaultsKeys.keyOne)
let ref = FIRDatabase.database().reference().child("images").child("\(userUuid!)")
let filterQuery = ref.queryOrdered(byChild: "uuid").queryEqual(toValue: "\(uuid)") // where uuid is a value from another view

filterQuery.observe(.value, with: { (snapshot) in
        for images in snapshot.children {
            print(images)
        }
})

But I receive nothing. 但是我什么也没收到。 I want to read the images' links to show them in the view controller. 我想阅读图像的链接以在视图控制器中显示它们。

  1. Make sure that the uuid var in the line below is not an optional value (or if it is, unwrap it) because otherwise you'll be querying to compare to "Optional(myUuidValue)" instead of "myUuidValue" 确保下面一行中的uuid var不是一个可选值(如果是,请解开它),因为否则,您将查询比较"Optional(myUuidValue)"而不是"myUuidValue"

     let filterQuery = ref.queryOrdered(byChild: "uuid").queryEqual(toValue: "\\(uuid)") 
  2. The snapshot in the line below contains more than just the images, it has all the other children under that uuid snapshot行中的snapshot不仅包含图像,还包含该uuid下的所有其他子级

     filterQuery.observe(.value, with: { (snapshot) in }) 

    So extract the images like this: 因此,像这样提取图像:

     filterQuery.observe(.value, with: { (snapshot) in let retrievedDict = snapshot.value as! NSDictionary let innerDict = retrievedDict["KeyHere"] as! NSDictionary // the key is the second inner child from images (3172FDE4-...) let imagesOuterArray = userDict["images"] as! NSArray for i in 0 ..< imagesOuterArray.count { let innerArray = imagesOuterArray[i] as! NSArray for image in innerArray { print(image as! String) } } }) 

    Clarification: cast all the children of the uuid as an NSDictionary , then extract the nested arrays using those two for-loops 澄清:将uuid的所有子代转换为NSDictionary ,然后使用这两个for循环提取嵌套数组

Update Thanks to Jay for pointing out the error! 更新感谢杰伊指出错误! Also, as Jay suggested, consider restructuring your database and replacing those arrays with dictionaries that perhaps contain the URL, path (for deleting purposes if you need that), and timestamp of each image. 另外,如Jay所建议的那样,请考虑重组数据库,并用可能包含URL,路径(如果需要的话,用于删除)和每个图像的时间戳的字典替换这些数组。

After strugling for the answer, got this code works 努力寻找答案后,此代码有效

let ref = FIRDatabase.database().reference().child("images").child("\(userUuid!)")
    let filterQuery = ref.queryOrdered(byChild: "identifier").queryEqual(toValue: "\(identifier)")

    filterQuery.observe(.value, with: { (snapshot) in
        for child in snapshot.children {
            if (child as AnyObject).hasChild("images") {
                let images = (images as AnyObject).childSnapshot(forPath: "images").value! as! NSArray
                for i in images {
                    for j in i as! [AnyObject] {
                        let url = NSURL(string: j as! String)
                        //Then downloaded the images to show on view
                        URLSession.shared.dataTask(with: url! as URL, completionHandler: { (data, response, error) in
                            if error != nil {
                                print(error)
                                return
                            }
                            //Code to show images..

                        }).resume()
                    }


                }
            }
        }

    })

Can i receive feedback about this? 我可以收到有关此问题的反馈吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM