Firebase database Not equal request - Alternative solution (for iOS)

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 07:26:46

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/37752402/firebase-database-not-equal-request-alternative-solution-for-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!