3
votes

I need to create a UISearchController using firebase as the backend. I currently have two users in firebase. One user has made one post and the other has made four posts. I will like to be able to search for the title of all of the books in my database (which is five). However, as of now, I can only search for the books the current signed in user uploaded. Below is a screenshot of what my database looks like and what my code currently looks like.

Here is a screenshot of what my database looks like

databaseRef.child("posts").child(userID!).observe(.childAdded, with: { (snapshot) in


    let key = snapshot.key
    let snapshot = snapshot.value as? NSDictionary
    snapshot?.setValue(key, forKey: "uid")

    if(key == self.loggedUser?.uid)
    {
        print("same as logged in user")
    }
    else
    {
    self.usersArray.append(snapshot)
         self.followUsersTableView.insertRows(at: [IndexPath(row:self.usersArray.count-1,section:0)], with: UITableViewRowAnimation.automatic)
    }



    }) { (error) in
        print(error.localizedDescription)
    }

}
1

1 Answers

2
votes

You will need a different parent node called Books :-

Right now your JSON is something like this :-

posts : {
    userID1 : {
       BOOK_11_ID : {.....}
       };

    userID2 : {
       BOOK_21_ID : {.....},
       BOOK_22_ID : {.....},
       BOOK_23_ID : {.....},
       BOOK_24_ID : {.....},
       }
 }

You gotta modify your database JSON structure to :-

 posts : {

   Users_books :{
    userID1 : {
       BOOK_11 : true  // Value 'true' means that this user 
                      // has subscribed or bought this book or whatever
       };

    userID2 : {
       BOOK_21 : true,
       BOOK_22 : true,
       BOOK_23 : true,
       BOOK_24 : true,
       }
      },

   Books:{
       BOOK_11_ID : {/Book details/};
       BOOK_21_ID : {/Book details/};
       BOOK_22_ID : {/Book details/};
       BOOK_23_ID : {/Book details/};
       BOOK_24_ID : {/Book details/};
       }

 }

Modify your security rules for section 'Book' to make is visible to all the authenticated users.

{
 "rules" :{

  "Users_books" : {

     ".write" : "auth != null && auth.uid === $uid",  // Will allow only the user to make any change within its node and no-one else
     ".read": "auth != null && auth.uid === $uid"

       },
  "Books" : {
      ".write" : "auth != null",  // Will allow all the users that are authenticated to your app to access every books detail.
      ".read" : "auth != null"
       }
     } 
  }

Now what you gotta do is :-

1) If its the user that is creating the book ; Then store the book details in the parent node 'Books' under the books uid and set the value of book_ID to true in the users node.

2) If the database is all backend i.e you input it; Whenever the user subscribe's or buys a books just fetch the uid of that book and set it to true in the section of that user.

PS : Always fan out your database ; its much easier to search from it .Although i am pretty sure you are gonna wanna store some personal details of the user in the near future so for that just make another node . Name it 'Users', security rules will be same as 'Users_books' and under 'Users' parent node append details of every which user with their uid as their key.