问题描述
{
post-评论:{
post-id-1:{
comment-id-11:{
author:user1,
text: Hello world,
uid:user-id-2
},....
}
我想拉取所有的评论,但不包括当前用户的一个。
在SQL中,这将被翻译进入:
从后注释中选择*其中id!=user-id-2
据我所知,Firebase数据库不提供排除方法节点基于值的存在(即:用户ID!= ...)。
因此,是否有其他解决方案来解决这个问题。可以通过更改数据库结构,也可以在数据加载完成后处理数据源。
对于后者,我使用的是FirebaseTableViewDataSource。是否有一种方法来过滤查询后的数据?
非常感谢
$ $ p $
let commentsRef = self.myRootRef.childByAppendingPath(comments)
commentsRef.observeEventType(.ChildAdded,withBlock:{snapshot in
let uid = snapshot.value [uid] as!String
if uid!= current_uid {
// do stuff
}
})
你可以在这里进行扩展,并通过.Value载入所有内容,并在代码中遍历子元素。该方法将取决于您正在加载多少个节点.ChildAdded将会降低内存使用量。
I am using Firebase database with a Json structure to manage users' comments.
{
"post-comments" : {
"post-id-1" : {
"comment-id-11" : {
"author" : "user1",
"text" : "Hello world",
"uid" : "user-id-2"
},....
}
I would like to pull all the comments but excluding the current user's one.
In SQL this will be translated into:Select * from post-comments where id !="user-id-2"
I understand that Firebase database does not offer a way to excludes nodes based on the presence of a value (ie: user id != ...).
Thus is there any alternative solutions to tackle this. Either by changing the Database structure, of maybe by processing the datasource once the data are loaded.
For the latter I am using a FirebaseTableViewDataSource. is there a way to filter the data after the query?
Thanks a lot
The first solution is to load the comments via .ChildAdded and ignore the ones with the current user_id
let commentsRef = self.myRootRef.childByAppendingPath("comments")
commentsRef.observeEventType(.ChildAdded, withBlock: { snapshot in
let uid = snapshot.value["uid"] as! String
if uid != current_uid {
//do stuff
}
})
You could expand on this and load everything by .Value and iterate over the children in code as well. That method will depend on how many nodes you are loading - .ChildAdded will be lower memory usage.
这篇关于Firebase数据库不等于请求 - 替代解决方案(适用于iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!