gpt4 book ai didi

android - 强制 Firebase 使用缓存数据

转载 作者:太空狗 更新时间:2023-10-29 16:12:25 25 4
gpt4 key购买 nike

我想强制 Firebase 给我缓存数据,从服务器获取最新数据。这可能吗?

想法是启用持久性,在特定位置调用 keepSynced(true),添加一个监听器以在数据可用时收到通知,然后调用 keepSynced(false) 以确保 future 的监听器获得缓存的数据。

但是,在测试应用程序中,这似乎不起作用。调用 keepSynced(false) 不会阻止监听器从服务器获取最新消息。 (下面的代码)

处理一组暂时不变的 Firebase 数据的正确方法是什么?

public class MainActivity extends AppCompatActivity {
private static String TAG = "tag";
Button keepSyncedTrue;
Button keepSyncedFalse;
Button getData;
TextView data;
private DatabaseReference myRef;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
myRef = getDatabaseReference();
setContentView(R.layout.activity_main);
keepSyncedTrue = (Button)findViewById(R.id.keepSyncTrue);
keepSyncedFalse = (Button)findViewById(R.id.keepSyncFalse);
getData = (Button)findViewById(R.id.getData);
data = (TextView)findViewById(R.id.data);

keepSyncedTrue.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
myRef.keepSynced(true);
}
});

keepSyncedFalse.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
myRef.keepSynced(false);
}
});

getData.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
// Read from the database
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + value);
data.setText(value);
}

@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
}
});

}

private DatabaseReference getDatabaseReference() {
return FirebaseDatabase.getInstance().getReference("message");
}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin">


<Button
android:id="@+id/keepSyncTrue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="keepSynced(true)" />

<Button
android:id="@+id/keepSyncFalse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="keepSynced(false)" />

<Button
android:id="@+id/getData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="get data" />

<TextView
android:id="@+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

最佳答案

这取决于您的最终目标。

如果您想避免任何类型的同步,您可以使用

DatabaseReference.goOffine();

正如您在 the documentation 中看到的那样:

Manually disconnect the Firebase Database client from the server and disable automatic reconnection.

Note: Invoking this method will impact all Firebase Database connections.

否则:

FirebaseDatabase.goOffline();

如所述in doc :

Shuts down our connection to the Firebase Database backend until goOnline() is called

此外,如果您想要保持同步并使用本地缓存数据,您可以使用:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

如文档中所述:

By enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache. Listener callbacks will continue to fire for local updates.

在这种情况下,您的应用程序使用缓存数据,但客户端继续与服务器数据同步。
More info here.

同样在这种情况下保持特定位置同步使用:

yourRef.keepSynced(true);

关于android - 强制 Firebase 使用缓存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42480021/

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