0

Implemented in the application firebase authentication. As well as the ability to add and delete records. But when I go with another account, the records are the same everywhere.

Q: how can I create my own records for each authorized user?

Now have smth like this:

enter image description here

As I understood from the comment I need to add another field to my structure: "users". And ultimately get type target-list - > users - > targets

But for example in my main activity I select as follows: databaseReference = FirebaseDatabase.getInstance().getReference("targets")

How do I understand here I have to change the structure?

UPD: I tried to to next:

private fun updateListData() {
    val uid = FirebaseAuth.getInstance().currentUser?.uid ?: ""
    databaseReference = FirebaseDatabase.getInstance().getReference("targets").child(uid)
    getTargetsFromDb()
}

and in getTargetsFromDb():

private fun getTargetsFromDb() {
    databaseReference?.addValueEventListener(object : ValueEventListener {
        override fun onDataChange(dataSnapshot: DataSnapshot) {
            for (targetSnapshot in dataSnapshot.children) {
                val target = targetSnapshot.getValue(Target::class.java)
                target?.let { targetList.add(it) }
            }
            recyclerView?.adapter = adapter
        }

        override fun onCancelled(databaseError: DatabaseError) {
            Log.d("some", "Error trying to get targets for ${databaseError.message}")
        }
    })
}

And get next structure:

enter image description here

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Morozov
  • 4,968
  • 6
  • 39
  • 70
  • You have to add a one layer on top of targets with the user-id key. Doing this every user have their own list of targets – Murdok Mar 26 '19 at 11:59
  • @Murdok thx, I have updated the question, if not difficult, please see – Morozov Mar 26 '19 at 12:08
  • Why are you using as the unique identifier a pushed id rather than the `uid` that is coming from the authentication process? – Alex Mamo Mar 26 '19 at 12:22
  • @AlexMamo But how can I use the one that comes from the authentication process? – Morozov Mar 26 '19 at 12:25
  • @AlexMamo I so understand I can use it in `onActivityResult()` when I successfully pass authorization? – Morozov Mar 26 '19 at 12:27
  • As explained **[here](https://stackoverflow.com/questions/49253026/firebase-auth-and-database/49256810)** and in Kotlin `val uid = FirebaseAuth.getInstance().currentUser!!.uid` once you are authenticated. – Alex Mamo Mar 26 '19 at 12:29
  • @AlexMamo Updated question, but I miss something – Morozov Mar 26 '19 at 12:54
  • Shouln't be `.getReference("users")`? The uid is used under `Users` node right? Not under `targets`. – Alex Mamo Mar 26 '19 at 13:02
  • @AlexMamo yep, i made smth like this: `if (resultCode == Activity.RESULT_OK) { val uid = FirebaseAuth.getInstance().currentUser?.uid ?: "" val userName = FirebaseAuth.getInstance().currentUser?.displayName val userEmail = FirebaseAuth.getInstance().currentUser?.email val userModel = User(userName, userEmail) val rootRef = FirebaseDatabase.getInstance().reference val usersRef = rootRef.child("users") usersRef.child(uid).setValue(userModel)` – Morozov Mar 26 '19 at 13:05
  • That's good. The result in the database is as expected. – Alex Mamo Mar 26 '19 at 13:06
  • @AlexMamo no problem with this. The problem is to create targets for each user – Morozov Mar 26 '19 at 13:08
  • In that case, why aren't you changing that pushed id with the `uid`? Will solve the problem. – Alex Mamo Mar 26 '19 at 13:12
  • @AlexMamo but i use here: `val uid = FirebaseAuth.getInstance().currentUser?.uid ?: "" databaseReference = FirebaseDatabase.getInstance().getReference("targets").child(uid)` – Morozov Mar 26 '19 at 13:14
  • I the screenshot that you provided, you aren't. I see those pushed ids. – Alex Mamo Mar 26 '19 at 13:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190699/discussion-between-morozov-and-alex-mamo). – Morozov Mar 26 '19 at 13:23

1 Answers1

0

Accodring to your comments and messages, you want to create targets for each user separately. Using your actual structure you have the users and targets separately. To solve this, I recommend you another database structure that looks like this:

Firebase-root
  |
  --- targets
       |
       --- uid
            |
            --- pushedId
            |    |
            |    --- description: "some description"
            |    |
            |    --- name: "Vadim"
            |
            --- pushedId
                 |
                 --- description: "another description"
                 |
                 --- name: "Morozov"

To get all the targets that corresponde to a particular user, simply use the following query:

val uid = FirebaseAuth.getInstance().currentUser!!.uid
val rootRef = FirebaseDatabase.getInstance().getReference()
val uidRef = rootRef.child("targets").child(uid)
uidRef.addListenerForSingleValueEvent(/* ... */);

An alternative database structure might be:

Firebase-root
  |
  --- targets
        |
        --- pushedId
        |    |
        |    --- description: "some description"
        |    |
        |    --- name: "Vadim"
        |    |
        |    --- uid: "VDMQ ... 0nw1"
        |
        --- pushedId
             |
             --- description: "another description"
             |
             --- name: "Svetlana"
             |
             --- uid: "eluk ... PJu1"

As you can see, it has a level less than the previous structure. Now, to get all the targets that corresponde to a particular user, please use the following query:

val uid = FirebaseAuth.getInstance().currentUser!!.uid
val rootRef = FirebaseDatabase.getInstance().getReference()
val targetsRef = rootRef.child("targets").orderByChild("uid").equalTo(uid)
targetsRef.addListenerForSingleValueEvent(/* ... */);

So it's up to you to decide which one is better for you.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • in the second case you get the Query for `targetsRef`. In the first case, alas, still another structure is obtained. When authenticating, do I need to create a user in the database? – Morozov Mar 26 '19 at 14:21
  • 1
    Sure you need to create a user in the database. I didn't add the `users` node because of space but you definetely should add it. – Alex Mamo Mar 26 '19 at 14:27
  • Hi Morozov! Is there everything alright, can I help you with other informations? – Alex Mamo Mar 27 '19 at 09:45
  • Updated question. Can u check please? Maybe I should share my test project. in git as an example? – Morozov Mar 27 '19 at 16:01
  • As you probably know, the rule in this community is to ask a question and if you get an answer, to solve your problem you should make your own attempt given the information in the answer and ask another question if something else comes up. So I rolled back to the previous version. – Alex Mamo Mar 27 '19 at 16:17
  • as you can see from the question that I updated, your decision did not help me, apparently I'm missing something, but alas, to determine the error has not yet happened – Morozov Mar 28 '19 at 10:00
  • Editing your question over and over again with every problem that you encounter is not a solution. That's why I recommended you to ask another question, in the moment you encounter a new problem. We stick to one question at a time. – Alex Mamo Mar 28 '19 at 10:07
  • I've updated the question to reflect your notes. The problem remained the same – Morozov Mar 28 '19 at 10:37
  • Between the time you get a solution to a problem and when you actually solve it, you can face many other coding challenges and in the same time, the main problem can remain the same. So neither me nor the other users can help you with all the problems that you face during your development, that's why you should take the time and ask a separate question for every problem separately. – Alex Mamo Mar 28 '19 at 10:49
  • I rolled back your question because the database structure has been modified due the changes that you made in your code. So those changes become a new problem that must be treated separately. Hope you undestand this point of view. So if you want to get help from me or other Firebase developers, please post a separate question using a [MCVE](https://stackoverflow.com/help/mcve) for every issue separately. – Alex Mamo Mar 28 '19 at 10:52
  • Is there everything alright, have you solved the case? – Alex Mamo Apr 03 '19 at 09:53