kotlin 我的代码中有一些错误,我想在firebase列表中添加1,但它一直在添加,没有停止

yzuktlbb  于 2022-12-23  发布在  Kotlin
关注(0)|答案(1)|浏览(116)

'

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) {

            }
        })
    }
}

'
在下面的代码中,它根本不添加。我只想添加1次,你有什么建议
'

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))
            }
    }
}

'
在代码1中,我的代码中有一些错误。我想在firebase列表中添加1,但它一直在添加,没有停止
在代码2和下面的代码中,它根本没有添加。我只想添加1次,你有什么建议

oprakyz7

oprakyz71#

在第一个代码中,更改

addValueEventListener()

addListenerForSingleValueEvent()

最终输出

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) {

            }
        })
    }
}

相关问题