ios - Firebase Database indexPath -
i'm trying retrieve data firebase using function i'm not getting data:
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 } } }
}
my problem index.count equal 1 it's supposed equal 12.
here's original code:
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 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?]() 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() }
so i'm trying same thing firebase. if need more details please ask!
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]
could please attach output of print(dictionary)
, print(snapshot.value)
before calling artviewmodelcontroller.parse(dictionary)
?
var arts = [art?]() print(dictionary) // -> output #1 print(snapshot.value) // -> output #2 if let art = artviewmodelcontroller.parse(dictionary) { arts.append(art) [...]
Comments
Post a Comment