skip to Main Content

i am using two collections in firebase firestore post collection and users collection like below image:

firebase post collection and user collection createdBy is user i want to change this in each and every post of the same user

If a user changes its own profile(like name,age etc) information, his/her information in the user collection is updated, and in future all the post will made by this user will also save the updated information regarding the user. But in the posts made in the past by this user are still having the old information of this user, unlike social media apps in which if we change our profile information started reflacting on every post.

To avoid this, I get author (user) of the post from users collection using uid and show these information on the ui, like below:

GlobalScope.launch {
        val userDao = UserDao()
        val user = userDao.getUserById(model.uid).await().toObject(User::class.java)!!
        withContext(Main) {
            imageUserName.text = user.displayName
            Glide.with(imageUserImage.context).load(user.imageUrl).circleCrop().into(imageUserImage)
        }
    }
}

but here i am using coorutine inside adapter, and I do not want that. I am sure there will be a better way than this.

Please tell me the right way to update the users information. Currently i am doing this like this:

val saveBtn:Button = dialog.findViewById(R.id.save_btn)

    saveBtn.setOnClickListener {

        val name = nameEdit.editableText.toString()
        val cource = courceEdit.editableText.toString()
        val bio = bioEdit.editableText.toString()

        if(name.isEmpty()){

            Toast.makeText(this,"pleas write your name",Toast.LENGTH_LONG).show()

        }else{

            GlobalScope.launch {
            val db = FirebaseFirestore.getInstance()
                val map = mapOf("displayName" to name, "cource" to cource,"bio" to bio)

            db.collection("users").document(uid).update(map)
           dialog.dismiss()


                val user = mUserDao.getUserById(uid).await().toObject(User::class.java)!!

                withContext(Main){
                    upDateUi(user)
                }
            }
        }
    }

I think that when i am storing a post then i should save createdBy(user) in some other way.

Currently i am doing this:

private fun uploadPost() {

    val title = post_title.text.toString()
    if(title == ""){

        Toast.makeText(this,"write something",Toast.LENGTH_LONG).show()
        return
    }
    val dialog = Dialog(this)
    dialog.setContentView(R.layout.progress_dialog)
    dialog.show()
    post_btn.isEnabled = false
    val post = Post(
        currentUserId,
        title,
        currentUser,
        "",
        "",
        "",
        System.currentTimeMillis(),
        0
    )

    db.collection("posts").add(post)
        .addOnCompleteListener{postCompleteTask->
            if (postCompleteTask.isSuccessful == false) {
                Log.e(TAG, "exception during post ${postCompleteTask.exception}")
                Toast.makeText(
                    this,
                    "something went wrong in uploading post",
                    Toast.LENGTH_LONG
                ).show()
            }
            post_btn.isEnabled = true
            dialog.dismiss()
            finish()
        }
    }
}

I want to change users profile information like we change on instagram and facebook or LinkedIn.

2

Answers


  1. If you want the historical posts documents from the user to reflect their updated profile information, you will need to update all those documents individually. There is no special operation to update all the posts documents for the user, so it’s a matter of:

    1. Using a query to load all the posts documents for the user.
      If you have a lot of posts documents, this may require paginating through the results in batches.
    2. Looping over the individual results.
    3. Updating each document in turn with the new information.

    I recommend also checking out these related posts:

    Login or Signup to reply.
  2. there is a way, you can use firebase cloud functions.

    exports.updateUserInPosts = functions.firestore
        .document('users/{userId}')
        .onUpdate((change, context) => {
    

    this event will be triggered every time a user update his profile. you can then run a loop and change the name in all posts made by this user.

    P.s. why not keep just the user uid in the post and fetch the user by this uid when you fetch the posts?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search