我正在浏览Doug Stevenson(Firebase开发者倡导者)的博客,该博客讨论了如何将Firebase实时数据库与android架构组件一起使用。
有一个类Firebase QueryLiveData
,它形成了一个可重用的类来管理所有Firebase查询以及实现LiveData。虽然这完全适用于Firebase RealTime数据库,但我似乎无法更改或更改它以支持云fiRecovery数据库。
这是代码
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());
}
}
}
献给Kotlin的恋人:)
import android.os.Handler
import androidx.annotation.Nullable
import androidx.lifecycle.LiveData
import com.google.firebase.firestore.*
import timber.log.Timber
class FirebaseQueryLiveData(private val query: Query) : LiveData<QuerySnapshot>() {
private val listener = MyValueEventListener()
private var listenerRegistration: ListenerRegistration? = null
private var listenerRemovePending = false
private val handler = Handler()
private val removeListener = Runnable {
listenerRegistration!!.remove()
listenerRemovePending = false
}
override fun onActive() {
super.onActive()
Timber.d( "onActive")
if (listenerRemovePending) {
handler.removeCallbacks(removeListener)
} else {
listenerRegistration = query.addSnapshotListener(listener)
}
listenerRemovePending = false
}
override fun onInactive() {
super.onInactive()
Timber.d("onInactive: ")
// Listener removal is schedule on a two second delay
handler.postDelayed(removeListener, 2000)
listenerRemovePending = true
}
private inner class MyValueEventListener : EventListener<QuerySnapshot> {
override fun onEvent(@Nullable querySnapshot: QuerySnapshot?, @Nullable e: FirebaseFirestoreException?) {
if (e != null) {
Timber.e(e, "Can't listen to query snapshots: %s", querySnapshot)
return
}
value = querySnapshot
}
}
}
这是@JobM post的查询版本。谢谢@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);
}
}
}
我正在探索新的Android架构组件,并希望将其实现到一个Android应用程序中。当我在iOS开发中使用MVVM时,我对MVVM非常了解。阅读了Google提供的Android Architecture Components guide:https://developer.Android.com/topic/libraries/Architecture/guide.html 我有几个问题...
我目前正在学习新的Android导航架构组件 (https://developer.android.com/topic/libraries/architecture/navigation/). 我有点混淆了它的动机和概念,以下是我的不确定性: Android导航架构组件是否旨在消除在单个应用程序中使用多个活动的需要?这意味着,整个应用程序只需要一个单一活动,所有其他页面都将是片段? 在应用程序中使
我正在使用Kotlin,kapt和Android架构组件。当我构建项目时,一切似乎都很好,但在试图在设备/模拟器上运行应用程序后,Gradle的:Assemble任务抛出以下错误: 警告:警告:注释处理器“Android.arch.persistence.room.RoomProcessor”支持的源版本“release_7”小于-source“1.8” 更新 模块构建。在此处分级 项目生成。gr
我知道如何使用ViewModel,Repository和Room将数据从数据库传输到屏幕。但是如何使用POST方法实现登录活动。我需要从ViewModel或AuthorizationRepository创建LiveData isAuthorated吗?有人能展示在android架构组件中使用命令方法的示例吗?
注意:如果您已经在使用RxJava或Agera这样的库,那么您可以继续使用它们,而不是LiveData。但是当您使用它们或其他方法时,请确保您正确地处理了生命周期,以便在相关的LifecycleOwner停止时数据流暂停,并且在LifecycleOwner被销毁时数据流被销毁。您还可以添加android.arch.lifecycle:reActiveStreams工件,以便将LiveData与另一