【发布时间】:2017-08-20 01:08:31
【问题描述】:
我正在尝试使用此函数从 firebase 检索数据,但我没有获得所有数据:
func retrieveVideos(_ completionBlock: @escaping (_ success: Bool, _ error: Error?) -> ()) {
let userMessagesRef = DataService.instance.REF_ARTISTARTS.child(Auth.auth().currentUser!.uid)
userMessagesRef.observe(.childAdded, with: { [weak self] (snapshot) in
if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
print("SNAPSHOT: (snapshot.count)") = 12
}
guard let strongSelf = self else {return}
guard let dictionary = snapshot.value as? [String: AnyObject] else {
completionBlock(false, nil)
return
}
var arts = [Art?]()
if let art = ArtViewModelController.parse(dictionary) {
arts.append(art)
print(arts.count) = 1
}
strongSelf.viewModels = ArtViewModelController.initViewModels(arts)
completionBlock(true, nil)
}) { (error: Error) in
completionBlock(false, error)
}
}
var viewModelsCount: Int {
return viewModels.count
}
func viewModel(at index: Int) -> ArtViewModel? {
guard index <= 0 && index < viewModelsCount else { return nil }
return viewModels[index]
}
}
private extension ArtViewModelController {
static func parse(_ dictionary: [String: Any]) -> Art? {
let artID = dictionary["artID"] as? String ?? ""
let imgUrl = dictionary["imageUrl"] as? String ?? ""
let title = dictionary["title"] as? String ?? ""
let description = dictionary["description"] as? String ?? ""
let price = dictionary["price"] as? NSNumber ?? 0
let type = dictionary["type"] as? String ?? ""
let height = dictionary["height"] as? NSNumber ?? 0
let width = dictionary["width"] as? NSNumber ?? 0
let postDate = dictionary["postDate"] as? NSNumber ?? 0
return Art(artID: artID, imgUrl: imgUrl, price: price, title: title, description: description, type: type, height: height, width: width, postDate: postDate)
}
static func initViewModels(_ arts: [Art?]) -> [ArtViewModel?] {
print("SECOND VIDEO COUNT: \(arts.count)")
return arts.map { art in
if let art = art {
return ArtViewModel(art: art)
} else {
return nil
}
}
}
}
我的问题是我的 index.count 总是等于 1 但它应该等于 12。
这是原始代码:
func retrieveUsers(_ completionBlock: @escaping (_ success: Bool, _ error: NSError?) -> ()) {
let urlString = "http://localhost:3000/users"
let session = URLSession.shared
guard let url = URL(string: urlString) else {
completionBlock(false, nil)
return
}
let task = session.dataTask(with: url) { [weak self] (data, response, error) in
guard let strongSelf = self else { return }
guard let data = data else {
completionBlock(false, error as NSError?)
return
}
let error = NSError.createError(0, description: "JSON parsing error")
if let jsonData = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[String: AnyObject]] {
guard let jsonData = jsonData else {
completionBlock(false, error)
return
}
var users = [User?]()
for json in jsonData {
if let user = UserViewModelController.parse(json) {
users.append(user)
}
}
strongSelf.viewModels = UserViewModelController.initViewModels(users)
completionBlock(true, nil)
} else {
completionBlock(false, error)
}
}
task.resume()
}
所以我正在尝试使用 Firebase 做同样的事情。如果您需要更多详细信息,请询问!
快照打印:
SNAPSHOT: Optional({
description = "Yes ";
height = 87;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/medici-b6f69.appspot.com/o/Art%2F4bjurh5FFNOUb2D4oHGfRqEm7Il2%2FB6E2A9F0-C409-4732-B6D9-14382E6796F5?alt=media&token=32992b9c-4a08-456d-8950-119f681b4cdc";
postDate = 1498921599547;
price = 23;
private = 0;
title = "Yes ";
type = Modern;
userUID = 4bjurh5FFNOUb2D4oHGfRqEm7Il2;
width = 71;
})
SNAPSHOT: Optional({
artHeight = 85;
artWidth = 123;
description = "Yes ";
height = 85;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/medici-b6f69.appspot.com/o/Art%2F4bjurh5FFNOUb2D4oHGfRqEm7Il2%2F16280189-5AEA-47CB-9251-C95635FFE56C?alt=media&token=8198df4e-da8c-4883-b278-fc7c14730f69";
postDate = 1498921623601;
price = 23;
private = 0;
title = "blacks don\U2019t Crack ";
type = Abstract;
userUID = 4bjurh5FFNOUb2D4oHGfRqEm7Il2;
width = 123;
})
SNAPSHOT: Optional({
artHeight = 81;
artWidth = 11;
description = "Yes ";
height = 81;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/medici-b6f69.appspot.com/o/Art%2F4bjurh5FFNOUb2D4oHGfRqEm7Il2%2F305FDE5C-1C5C-4ABD-B4A1-4FA224421202?alt=media&token=fcba57df-e252-47c2-be92-7836bd97e6fe";
postDate = 1502242913081;
price = 23;
private = 0;
title = "Title ";
type = type;
userUID = 4bjurh5FFNOUb2D4oHGfRqEm7Il2;
width = 118;
})
SNAPSHOT: Optional({
description = "Hey ";
height = 82;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/medici-b6f69.appspot.com/o/Art%2F4bjurh5FFNOUb2D4oHGfRqEm7Il2%2F04B00727-2AE4-41E3-BCA3-3980182C7F67?alt=media&token=cc1d6a92-1625-453c-bf1f-2ec84d6df51a";
postDate = 1503341277594;
price = 23;
private = 0;
title = "Title ";
type = Modern;
userUID = 4bjurh5FFNOUb2D4oHGfRqEm7Il2;
width = 124;
})
DIC: ["height": 87, "private": 0, "width": 71, "postDate": 1498921599547, "description": Yes , "title": Yes , "imageUrl": https://firebasestorage.googleapis.com/v0/b/medici-b6f69.appspot.com/o/Art%2F4bjurh5FFNOUb2D4oHGfRqEm7Il2%2FB6E2A9F0-C409-4732-B6D9-14382E6796F5?alt=media&token=32992b9c-4a08-456d-8950-119f681b4cdc, "userUID": 4bjurh5FFNOUb2D4oHGfRqEm7Il2, "price": 23, "type": Modern]
DIC: ["artWidth": 123, "height": 85, "private": 0, "artHeight": 85, "description": Yes , "postDate": 1498921623601, "width": 123, "imageUrl": https://firebasestorage.googleapis.com/v0/b/medici-b6f69.appspot.com/o/Art%2F4bjurh5FFNOUb2D4oHGfRqEm7Il2%2F16280189-5AEA-47CB-9251-C95635FFE56C?alt=media&token=8198df4e-da8c-4883-b278-fc7c14730f69, "title": blacks don’t Crack , "price": 23, "type": Abstract, "userUID": 4bjurh5FFNOUb2D4oHGfRqEm7Il2]
DIC: ["artWidth": 11, "height": 81, "private": 0, "artHeight": 81, "description": Yes , "postDate": 1502242913081, "width": 118, "imageUrl": https://firebasestorage.googleapis.com/v0/b/medici-b6f69.appspot.com/o/Art%2F4bjurh5FFNOUb2D4oHGfRqEm7Il2%2F305FDE5C-1C5C-4ABD-B4A1-4FA224421202?alt=media&token=fcba57df-e252-47c2-be92-7836bd97e6fe, "title": Title , "price": 23, "type": type, "userUID": 4bjurh5FFNOUb2D4oHGfRqEm7Il2]
DIC: ["height": 82, "private": 0, "width": 124, "postDate": 1503341277594, "description": Hey , "title": Title , "imageUrl": https://firebasestorage.googleapis.com/v0/b/medici-b6f69.appspot.com/o/Art%2F4bjurh5FFNOUb2D4oHGfRqEm7Il2%2F04B00727-2AE4-41E3-BCA3-3980182C7F67?alt=media&token=cc1d6a92-1625-453c-bf1f-2ec84d6df51a, "userUID": 4bjurh5FFNOUb2D4oHGfRqEm7Il2, "price": 23, "type": Modern]
【问题讨论】:
-
您可以尝试断点调试,并使用
po OBJECTNAME打印对象,看看哪一行可能不起作用? -
请添加打印(快照)然后你能显示它打印的内容吗?
-
你的问题解决了吗?
-
@user1000 不,我还在努力。
-
您检查过我的答案并相应地更新了代码吗?
标签: ios swift firebase firebase-realtime-database