作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在经历这个blog通过 Doug Stevenson (Firebase 开发者倡导者)该博客讨论了如何将 firebase 实时数据库 与 android 架构组件一起使用。
FirebaseQueryLiveData
类构成了一个可重用的类,用于管理所有 Firebase 查询并实现 LiveData。虽然这完全适用于 Firebase RealTime 数据库,但我似乎无法更改或更改它以支持云 firestore 数据库。
这是代码
public class FirebaseQueryLiveData extends LiveData<DataSnapshot> {
private static final String LOG_TAG = "FirebaseQueryLiveData";
private final Query query;
private final MyValueEventListener listener = new MyValueEventListener();
public FirebaseQueryLiveData(Query query) {
this.query = query;
}
public FirebaseQueryLiveData(DatabaseReference ref) {
this.query = ref;
}
@Override
protected void onActive() {
Log.d(LOG_TAG, "onActive");
query.addValueEventListener(listener);
}
@Override
protected void onInactive() {
Log.d(LOG_TAG, "onInactive");
query.removeEventListener(listener);
}
private class MyValueEventListener implements ValueEventListener {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
setValue(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(LOG_TAG, "Can't listen to query " + query, databaseError.toException());
}
}
}
最佳答案
这是@JobM 帖子的查询版本。谢谢@JobM!为了清楚起见,我建议将@JobM 的版本重命名为 FirebaseDocumentLiveData。
import android.arch.lifecycle.LiveData;
import android.os.Handler;
import android.util.Log;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.ListenerRegistration;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;
import javax.annotation.Nullable;
public class FirebaseQueryLiveData extends LiveData<QuerySnapshot> {
public static final String TAG = "FbaseQueryLiveData";
private Query query;
private final MyValueEventListener listener = new MyValueEventListener();
private ListenerRegistration listenerRegistration;
private boolean listenerRemovePending = false;
private final Handler handler = new Handler();
public FirebaseQueryLiveData(Query query) {
this.query = query;
}
private final Runnable removeListener = new Runnable() {
@Override
public void run() {
listenerRegistration.remove();
listenerRemovePending = false;
}
};
@Override
protected void onActive() {
super.onActive();
Log.d(TAG, "onActive");
if (listenerRemovePending) {
handler.removeCallbacks(removeListener);
}
else {
listenerRegistration = query.addSnapshotListener(listener);
}
listenerRemovePending = false;
}
@Override
protected void onInactive() {
super.onInactive();
Log.d(TAG, "onInactive: ");
// Listener removal is schedule on a two second delay
handler.postDelayed(removeListener, 2000);
listenerRemovePending = true;
}
private class MyValueEventListener implements EventListener<QuerySnapshot> {
@Override
public void onEvent(@Nullable QuerySnapshot querySnapshot, @Nullable FirebaseFirestoreException e) {
if (e != null){
Log.e(TAG, "Can't listen to query snapshots: " + querySnapshot + ":::" + e.getMessage());
return;
}
setValue(querySnapshot);
}
}
}
关于带有 Firebase 的 Android 架构组件,特别是 Firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49798814/
我已经可以在其中输入一些附加文本了mydomain/wiki/Special:UserLogin。我添加了一句话: In order to apply for an account send an m
有人可以解释以下脚本输出背后的逻辑吗? import numpy if(numpy.dtype(numpy.float64) == None): print "Surprise!!!!" 谢谢
是我还是 gmail bulls**t?在 outlook/浏览器上,我的电子邮件是完美的,但在 gmail 上,2 个表之间有一个空间,为什么?!?图片:http://i.imgur.com/srJ
我是一名优秀的程序员,十分优秀!