Mic*_*iyo 2 android firebase firebase-realtime-database
我想强制Firebase给我缓存数据而不是从服务器获取最新信息.这可能吗?
我们的想法是启用持久性,调用keepSynced(true)特定位置,添加侦听器以在数据可用时得到通知,然后调用keepSynced(false)以确保将来的侦听器获取缓存数据.  
但是,在测试应用程序中,这似乎不起作用.调用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();
正如您在文档中看到的那样:
手动断开Firebase数据库客户端与服务器的连接并禁用自动重新连接.
注意:调用此方法将影响所有Firebase数据库连接.
除此以外:
FirebaseDatabase.goOffline();
如doc中所述:
关闭我们与Firebase数据库后端的连接,直到
goOnline()被调用
如果您希望保持同步并使用本地缓存数据,则可以使用:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
如文件中所述:
通过启用持久性,即使用户或操作系统重新启动应用程序,Firebase实时数据库客户端在联机时仍会同步到磁盘并且可脱机使用的任何数据.这意味着您的应用程序可以像使用存储在缓存中的本地数据一样在线工作.监听器回调将继续触发本地更新.
在这种情况下,您的应用程序使用缓存数据,但客户端继续与服务器数据同步.
更多信息在这里.
同样在这种情况下,要保持特定位置同步使用:
yourRef.keepSynced(true);
| 归档时间: | 
 | 
| 查看次数: | 2691 次 | 
| 最近记录: |