I would like to force Firebase to give me cached data and not go get the latest from the server. Is this possible?
The idea was to enable persistence, call keepSynced(true)
on a particular location, add a listener to be notified when the data is available, then call keepSynced(false)
to ensure that future listeners get the cached data.
However, in a test app this doesn't seem to work. Calling keepSynced(false)
doesn't prevent a listener from getting the latest from the server. (Code below)
What is the correct way to work with a temporarily-unchanging set of Firebase data?
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");
}
}
and
<?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>