Yacine Yacine - 1 year ago 354
iOS Question

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

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

Jay Jay
Answer Source

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.