我尝试使用此功能从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] {
打印(" 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 PRINT:
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]
答案 0 :(得分:0)
在致电print(dictionary)
之前,您能否附上print(snapshot.value)
和ArtViewModelController.parse(dictionary)
的输出?
var arts = [Art?]()
print(dictionary) // -> output #1
print(snapshot.value) // -> output #2
if let art = ArtViewModelController.parse(dictionary) {
arts.append(art)
[...]
答案 1 :(得分:0)
使用此: -
func retrieveVideos(_ completionBlock: @escaping (_ success: Bool, _ error: Error?) -> ()) {
let userMessagesRef = DataService.instance.REF_ARTISTARTS.child(Auth.auth().currentUser!.uid)
userMessagesRef.observe(.value, with: { [weak self] (snapshot) in
if let snapshots = snapshot.children.allObjects as? [DataSnapshot]
{
print("SNAPSHOT: (snapshots.count)")
}
var arts = [Art]()
guard let strongSelf = self else {return}
for snapshot in snapshots {
let dictionary = snapshot.value as? [String: AnyObject]
if let art = ArtViewModelController.parse(dictionary) {
arts.append(art)
}
}
strongSelf.viewModels = ArtViewModelController.initViewModels(arts)
completionBlock(true, nil)
}) { (error: Error) in
completionBlock(false, error)
} }
答案 2 :(得分:0)
您可以尝试以下两个选项。
选项#1
这应该假设snapshot.value
返回完整字典(12个项目的数组):
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
guard let dictionary = snapshot.value as? [String: AnyObject] else {
completionBlock(false, nil)
return
}
guard let strongSelf = self else {return}
var arts = [Art?]()
if let art = ArtViewModelController.parse(dictionary) {
arts.append(art)
}
strongSelf.viewModels = ArtViewModelController.initViewModels(arts)
completionBlock(true, nil)
}) { (error: Error) in
completionBlock(false, error)
}
}
选项#2
如果可行,选项#1 是更简单的解决方案。否则,因为看起来每个DataSnapshot
的内容都是字典,所以以下代码可能有效:
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
guard let strongSelf = self else {return}
var arts = [Art?]()
for snapshotItem in snapshot.children.allObjects as? Dictionary {
let art = Art(snapshot: snapshotItem)
arts.append(art)
}
strongSelf.viewModels = ArtViewModelController.initViewModels(arts)
completionBlock(true, nil)
}
}) { (error: Error) in
completionBlock(false, error)
}
您应该将Art(snapshot: Dictionary)
添加到Art
类:
// Initialize Art from dictionary
func Art(snapshot: Dictionary) {
let artID = dictionary["userUID"] 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["artHeight"] as? NSNumber ?? 0
let width = dictionary["artWidth"] 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)
}
注意:我认为DataSnapshot
中您要查找的密钥是userUID
,因为我没有看到artID
(正如解析代码中所示)。