Home > Back-end >  There is some error in my code. I want to add 1 to the firebase list but it keeps adding without sto
There is some error in my code. I want to add 1 to the firebase list but it keeps adding without sto

Time:12-23

`

override fun plusDown(key: String, downloads: Downloads, result: (UiState<String>) -> Unit) {
    CoroutineScope(Dispatchers.IO).launch {
        var size=0
        myRef.getReference(FirebaseRealtimeDatabaseConstants.path_posts).child(key).child("downloads").addValueEventListener(object :ValueEventListener{
            override fun onDataChange(snapshot: DataSnapshot) {
                size = snapshot.children.count()
                myRef.getReference(FirebaseRealtimeDatabaseConstants.path_posts).child(key).child("downloads").child(size.toString()).setValue(
                    downloads
                )
                    .addOnSuccessListener {
                        result.invoke(UiState.Success("SUCCES"))
                        Log.d("DOWNLOADCOUNT", size.toString())
                    }
                    .addOnFailureListener {
                        result.invoke(UiState.Failure(it.message))
                    }
            }

            override fun onCancelled(error: DatabaseError) {

            }
        })
    }
}

`

and in the code below it does not add at all. I want to add only 1 time what do you advise

`

override fun plusDown(key: String, downloads: Downloads, result: (UiState<String>) -> Unit) {
    CoroutineScope(Dispatchers.IO).launch {
        var size=0
        myRef.getReference(FirebaseRealtimeDatabaseConstants.path_posts).child(key).child("downloads").addValueEventListener(object :ValueEventListener{
            override fun onDataChange(snapshot: DataSnapshot) {
                size = snapshot.children.count()
                
            }

            override fun onCancelled(error: DatabaseError) {

            }
        })
        myRef.getReference(FirebaseRealtimeDatabaseConstants.path_posts).child(key).child("downloads").child(size.toString()).setValue(
            downloads
        )
            .addOnSuccessListener {
                result.invoke(UiState.Success("SUCCES"))
                Log.d("DOWNLOADCOUNT", size.toString())
            }
            .addOnFailureListener {
                result.invoke(UiState.Failure(it.message))
            }
    }
}

`

In code 1 There is some error in my code. I want to add 1 to the firebase list but it keeps adding without stopping

In code 2 and in the code below it does not add at all. I want to add only 1 time what do you advise

CodePudding user response:

In your 1st code, change

addValueEventListener()

to

addListenerForSingleValueEvent()

Final Output

override fun plusDown(key: String, downloads: Downloads, result: (UiState<String>) -> Unit) {
    CoroutineScope(Dispatchers.IO).launch {
        var size=0
        myRef.getReference(FirebaseRealtimeDatabaseConstants.path_posts).child(key).child("downloads").addListenerForSingleValueEvent(object :ValueEventListener{
            override fun onDataChange(snapshot: DataSnapshot) {
                size = snapshot.children.count()
                myRef.getReference(FirebaseRealtimeDatabaseConstants.path_posts).child(key).child("downloads").child(size.toString()).setValue(
                    downloads
                )
                    .addOnSuccessListener {
                        result.invoke(UiState.Success("SUCCES"))
                        Log.d("DOWNLOADCOUNT", size.toString())
                    }
                    .addOnFailureListener {
                        result.invoke(UiState.Failure(it.message))
                    }
            }

            override fun onCancelled(error: DatabaseError) {

            }
        })
    }
}
  • Related