Firebase快照不会转换为字典 [英] Firebase snapshot not being cast to dictionary

查看:147
本文介绍了Firebase快照不会转换为字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个函数应该从firebase中的 comments节点中获取数据。我想实施分页,不要一次加载超过100条评论。一切似乎正在工作,但我的代码似乎失败在铸造 snapchat.value 到一个字典

  func fetchComments(){
messagesRef = Database.database()。reference()。child(Comments (eventKey)
var query = messagesRef?.queryOrderedByKey()
如果comments.count> 0 {
let value = comment.last?.commentID
query = query?.queryStarting(atValue:value)
}
query?.queryLimited(toFirst:2).observe (.childAdded,其中:{(snapshot)in

var allObjects = snapshot.children.allObjects as?[DataSnapshot]

allObjects?.forEach({(snapshot)in
//打印出来的快照并不是空的
print(snapshot.value)//这里它保留着else语句,即使snapshot.value显然存在

guard让commentDictionary = snapshot.value as?[String:Any] else {
return
}

print(commentDictionary)
})
} ){(err)in
print(Failed to observe comments)
}
}

我的问题是任何人都可以看看这个,也许看到我哪里错了?我的代码看起来很好,我看不出有什么问题。



我的树看起来像这样

 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:// firebases torage.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
}
}
}

基于我的代码,它绕过密钥并直接进入子节点。
例如,如果通过MIA,它应该去MIA,并抓住每个评论-KrrsXkj6FznzRD0-Xzs和-KrpoEnNYsmRZ5guORUj对应的密钥,但它将返回在该唯一ID下的所有内容。这是一个问题

解决方案

您的回调中的代码似乎假设您使用一组注释进行调用。要获得这样的集合,你需要观察 .value 事件。当您观察 .value 事件时,您将使用包含与查询匹配的所有节点的单个 snapshot 来调用回调:
$ b $ pre $ 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)
$ b $ guard let commentDictionary = snapshot.value as?[String:Any] else {
return
}

print( )(
})
}){(err)in
print(Failed to observe comments)
}
pre>

观察 .childAdded 时,您的回调将被调用,以符合查询的每个单独节点。这意味着你需要摆脱代码中的循环:

$ p $ query?.queryLimited(toFirst:2).observe( .childAdded,其中:{(snapshot)in
print(snapshot.key)
print(snapshot.value)

guard let commentDictionary = snapshot.value as?[String:任何]其他{
返回
}

print(commentDictionary)
$ b $}} {(err)in
print(Failed to观察评论)
}


I have this function that is supposed to fetch data from a comments node from firebase. I want to implement pagination to not load 100+ comments at once. Everything seems to be working but my code seems to be failing at casting the snapchat.value to a 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 and it isn't empty 
                print(snapshot.value) // here it keeps going into the else statement even though snapshot.value clearly exist.

                guard let commentDictionary = snapshot.value as? [String:Any] else{
                    return
                }

                print(commentDictionary)
            })
                   }) { (err) in
            print("Failed to observe comments")
        }
    }

My question is can anyone take a look at this and maybe see where I went wrong? My code looks fine to me and I can't see what's is wrong.

My tree looks like 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 my code it bypasses the key and goes straight to the children. For example if pass in MIA it should go to MIA and grab the key corresponding to each comment "-KrrsXkj6FznzRD0-Xzs" and "-KrpoEnNYsmRZ5guORUj" but it is returning everything under that unique ID instead. Which is a problem

解决方案

The code in your callback seems to assume that you get called with a collection of comments. To get such a collection you need to observe the .value event. When you observe the .value event, you callback gets invoked with a single snapshot that contains all the nodes matching the 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 to observe comments")
    }

When you observe .childAdded, your callback instead gets called for every individual node matching the query. That means you need to get rid of a loop in your 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 to observe comments")
    }

这篇关于Firebase快照不会转换为字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆