【发布时间】:2018-01-18 23:51:06
【问题描述】:
我正在尝试获取针对特定用户 (UserId) 的所有活动的所有 imageURL。快照的内容包括正确的条目,但是 imageURL 未附加到 imageArray。我做错了什么?
// Get all images for a specific user
func getImagesForUser(userId:String, handler: @escaping (_ imageArray: [String]) -> ()) {
var imageArray = [String]()
REF_ACTIVITY.queryOrdered(byChild: "userId").queryEqual(toValue: userId).observe(.value, with: { snapshot in
dump(snapshot)
guard let data = snapshot.value as? NSDictionary else { print("else was returned"); return }
let imageURL = data["imageURL"] as? String ?? ""
imageArray.append(imageURL)
handler(imageArray)
})
}
转储输出(快照)
Snap (-L2RDXTKANzD8YGpPl92) {
gearId = asfasfsf;
imageCount = 3;
imageURLs = {
1 = "http://www.foo.com";
2 = "http://www.foo2.com";
3 = "http://www.foo3.com";
};
killCount = 10;
kills = 12;
likeCount = 2;
likes = {
fas86q39rasf = 1;
};
userId = NhZZGwJQCGe2OGaNTwGvpPuQKNA2;
}
Snap (-L2RDXTKANzD8YGpPl92) {
gearId = asfasfsf;
imageCount = 3;
imageURLs = {
1 = "http://www.foo.com";
2 = "http://www.foo2.com";
3 = "http://www.foo3.com";
};
killCount = 10;
kills = 12;
likeCount = 2;
likes = {
fas86q39rasf = 1;
};
userId = NhZZGwJQCGe2OGaNTwGvpPuQKNA2;
}
更新 - Dict 的输出
24 elements
▿ 0 : 2 elements
- key : "commentCount"
- value : 1
// removed some info
...
▿ 12 : 2 elements
- key : "imageURLs"
▿ value : 4 elements
- 0 : <null>
- 1 : https://firebasestorage.googleapis.com/v0/b/shoota-179610.appspot.com/o/activity_image%2FCA6F4C93-8F5F-456C-9FD6-F2EACC444739?alt=media&token=522d957b-dedf-4bdc-9cbc-1de581284393
- 2 : https://firebasestorage.googleapis.com/v0/b/shoota-179610.appspot.com/o/activity_image%2FCA6F4C93-8F5F-456C-9FD6-F2EACC444739?alt=media&token=522d957b-dedf-4bdc-9cbc-1de581284393
- 3 : https://firebasestorage.googleapis.com/v0/b/shoota-179610.appspot.com/o/activity_image%2FCA6F4C93-8F5F-456C-9FD6-F2EACC444739?alt=media&token=522d957b-dedf-4bdc-9cbc-1de581284393
▿ 13 : 2 elements
- key : "imageURL"
- value : https://firebasestorage.googleapis.com/v0/b/shoota-179610.appspot.com/o/activity_image%2FCA6F4C93-8F5F-456C-9FD6-F2EACC444739?alt=media&token=522d957b-dedf-4bdc-9cbc-1de581284393
▿ 14 : 2 elements
- key : "killCount"
- value : 10
【问题讨论】:
-
由于文档有时缺少示例,这也是一个不错的起点:stackoverflow.com/search?q=%5Bfirebase%5D%5Bswift%5D+query
-
第二个问题:几千没问题,几万也没问题,当你达到几十万/百万时,事情变得更加有趣。见stackoverflow.com/questions/28857905/…、stackoverflow.com/questions/39712833/…
-
@FrankvanPuffelen:感谢这些链接。它们很有用。我已经用我的新代码更新了我的问题。我还有一些问题,但我认为我已经接近了......
标签: swift firebase firebase-realtime-database