gpt4 book ai didi

带有 Firebase 的 Android 架构组件,特别是 Firestore

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:57:27 28 4
gpt4 key购买 nike

我正在经历这个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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com