【发布时间】:2019-10-15 10:53:00
【问题描述】:
我正在制作 todolist 应用程序。 当我使用事务时,我有一个问题。
我尝试使用事务更改 todolist 用户数。 但是由于某种原因,两个活动之间的用户数发生了变化
所以,这就是那个代码
在 todolist 中添加用户
val todoListRef = firestore!!.collection("todoList").document(intent.getStringExtra("checkList_name"))
firestore?.runTransaction { transaction ->
var todoListDTO = transaction.get(todoListRef).toObject(TodoListDTO::class.java)
if (todoListDTO == null) {
todoListDTO = TodoListDTO()
transaction.set(todoListRef, todoListDTO)
return@runTransaction
}
if (!todoListDTO.todoListUsers.containsKey(currentUserUid!!)) {
todoListDTO.todoListUserCount = todoListDTO.todoListUserCount + 1
todoListDTO.todoListUsers[currentUserUid!!] = true
}
transaction.set(todoListRef, todoListDTO)
return@runTransaction
}
删除待办事项列表中的用户
val todoListRef = firestore!!.collection("todoList").document(checkListName!!)
firestore?.runTransaction { transaction ->
val todoListDTO = transaction.get(todoListRef).toObject(TodoListDTO::class.java)
todoListDTO!!.todoListUserCount = todoListDTO.todoListUserCount - 1
todoListDTO.todoListUsers.remove(currentUserUid!!)
transaction.set(todoListRef, todoListDTO)
return@runTransaction
}
我担心交易会崩溃。 如果一个人被添加了todolist用户数,同时另一个人被删除了todolist用户数,这会导致交易崩溃或不反映变化。 这样对吗?或者我可以对一个文件使用双重交易吗?
如果有问题,如何在不中断其他活动的事务的情况下更改单个文档数据?
永远谢谢你。
【问题讨论】:
标签: android kotlin transactions google-cloud-firestore