我使用事務在Firestore中實現了郵件喜歡和評論功能。我使用交易,因爲我需要在帖子的likes/comments子集合和更新計數器中添加新字段,並且還將帖子ID添加到用戶喜歡/評論的帖子集合中。Firestore runTransaction()和離線工作
我注意到,如果我下線,我要求我的文章像這樣一切都OK了:
val postDocRef = FirebaseUtil.postsColRef.document(postId)
postDocRef.get().addOnSuccessListener { doc ->
val post = doc.toObject(Post::class.java)
Timber.e(post.toString())
}
但是,如果我做相同的交易異常被拋出:
val postDocRef = FirebaseUtil.postsColRef.document(postId)
FirebaseUtil.firestore.runTransaction(Transaction.Function<Void> { transaction ->
val post = transaction.get(postDocRef).toObject(Post::class.java)
}
異常是:
com.google.firebase.firestore.FirebaseFirestoreException:UNAVAILABLE
爲什麼脫機模式在事務中不起作用?是否可以在離線狀態下實現此功能(在子集合中添加條目並更新不同對象中的字段)?
用continueWithTask()
替代交易鏈接有什麼缺點?