swift - Firebase snapshot not being cast to dictionary -
i have function supposed fetch data comments node firebase. want implement pagination not load 100+ comments @ once. seems working code seems failing @ casting snapchat.value dictionary
func fetchcomments(){ messagesref = database.database().reference().child("comments").child(eventkey) var query = messagesref?.queryorderedbykey() if comments.count > 0 { let value = comments.last?.commentid query = query?.querystarting(atvalue: value) } query?.querylimited(tofirst: 2).observe(.childadded, with: { (snapshot) in var allobjects = snapshot.children.allobjects as? [datasnapshot] allobjects?.foreach({ (snapshot) in // print out snapshot , isn't empty print(snapshot.value) // here keeps going else statement though snapshot.value exist. guard let commentdictionary = snapshot.value as? [string:any] else{ return } print(commentdictionary) }) }) { (err) in print("failed observe comments") } } my question can take @ , maybe see went wrong? code looks fine me , can't see what's wrong.
my tree looks this
"comments" : { "ccds" : { "-krrsxkj6fznzrd0-xzs" : { "content" : "shawn", "profileimageurl" : "https://firebasestorage.googleapis.com/v0/b/eventful-3d558.appspot.com/o/profile_images%2fbc868f8f-e9ec-4b9d-a248-dd2187bc140c.png?alt=media&token=fb14700c-2b05-4077-b45c-afd3de705801", "timestamp" : 1.503102381340935e9, "uid" : "oxgjbrhingbf7vbahpflhw6g7tb2" } }, "mia" : { "-krghz9d5_cpjkmdffef" : { "content" : "22", "profileimageurl" : "https://firebasestorage.googleapis.com/v0/b/eventful-3d558.appspot.com/o/profile_images%2ff50f6915-deab-4a5b-b1ab-cabc1e349148.png?alt=media&token=4eb7c708-ec87-45bf-952d-0bd410faee50", "timestamp" : 1.502915064803007e9, "uid" : "oxgjbrhingbf7vbahpflhw6g7tb2" }, "-krpoennysmrz5guoruj" : { "content" : "23", "profileimageurl" : "https://firebasestorage.googleapis.com/v0/b/eventful-3d558.appspot.com/o/profile_images%2fbc868f8f-e9ec-4b9d-a248-dd2187bc140c.png?alt=media&token=fb14700c-2b05-4077-b45c-afd3de705801", "timestamp" : 1.503067700479352e9, "uid" : "oxgjbrhingbf7vbahpflhw6g7tb2" } } } based off code bypasses key , goes straight children. example if pass in mia should go mia , grab key corresponding each comment "-krrsxkj6fznzrd0-xzs" , "-krpoennysmrz5guoruj" returning under unique id instead. problem
the code in callback seems assume called collection of comments. such collection need observe .value event. when observe .value event, callback gets invoked single snapshot contains nodes matching query:
query?.querylimited(tofirst: 2).observe(.value, with: { (snapshot) in var allobjects = snapshot.children.allobjects as? [datasnapshot] allobjects?.foreach({ (snapshot) in print(snapshot.key) print(snapshot.value) guard let commentdictionary = snapshot.value as? [string:any] else{ return } print(commentdictionary) }) }) { (err) in print("failed observe comments") } when observe .childadded, callback instead gets called every individual node matching query. means need rid of loop in code:
query?.querylimited(tofirst: 2).observe(.childadded, with: { (snapshot) in print(snapshot.key) print(snapshot.value) guard let commentdictionary = snapshot.value as? [string:any] else{ return } print(commentdictionary) }) { (err) in print("failed observe comments") }
Comments
Post a Comment