我正在使用Firebase Android SDK并且对发送同步请求而不是异步感兴趣.根据文档,在任何请求中都会显示回调.但是同步性呢?
谢谢!
我正在使用事务在Firestore中实现post喜欢和评论功能.我使用事务是因为我需要在帖子中的likes/comments子集和更新计数器中添加新字段,并且还将post id添加到用户喜欢/评论的帖子集合中.
我注意到如果我离线了,我就像这样请求我的帖子一切正常:
val postDocRef = FirebaseUtil.postsColRef.document(postId)
postDocRef.get().addOnSuccessListener { doc ->
val post = doc.toObject(Post::class.java)
Timber.e(post.toString())
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我在事务中执行相同的异常抛出:
val postDocRef = FirebaseUtil.postsColRef.document(postId)
FirebaseUtil.firestore.runTransaction(Transaction.Function<Void> { transaction ->
val post = transaction.get(postDocRef).toObject(Post::class.java)
}
Run Code Online (Sandbox Code Playgroud)
例外情况是:
com.google.firebase.firestore.FirebaseFirestoreException:UNAVAILABLE
为什么离线模式在交易中不起作用?是否可以在离线状态下实现此功能(在子集合中添加条目并在不同对象中更新字段)?
用continueWithTask()调用链替换事务有什么缺点?
我一直在parse-server用作我的后端,现在将我所有的东西迁移到FireStore(特别是用于存储额外的用户数据)。现在,通过 parse 我可以直接使用ParseUser.getCurrentUser().getString(...). 如果我使用FireStore,我已经创建了一个users集合,我可以document为每个用户存储一个集合,它还支持具有离线支持的本地存储。但我面临的问题是我总是需要使用异步/回调方法从document. 这是android中的一个例子:
final DocumentReference docRef = mFirestore
.collection(USER_COLLECTION).document(mAuthApi.getCurrentUserId());
docRef.get(Source.CACHE).addOnCompleteListener(task -> {
if(task.isSuccessful()) {
......
} else {
......
}
});
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以让我们DocumentReference 假设数据是本地缓存的,从而直接同步获取数据?就像是:
mFirestore.collection(USER_COLLECTION).document(mAuthApi.getCurrentUserId()).getString(...)
Run Code Online (Sandbox Code Playgroud)
这对我来说很容易,因为我需要ParseUser.getCurrentUser().getString(...)用FireStore整个代码库中的等效调用替换。