gpt4 book ai didi

android - FirebaseDatabase.getInstance().setPersistenceEnabled(true) 到底做了什么?

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:36 29 4
gpt4 key购买 nike

我似乎不理解 setPersistenceEnabled

我以为是通过调用

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

作为我的 Android 应用程序对象中的第一件事,即使在飞行模式下,我也可以在本地读取和写入 Firebase 数据,并且我的数据将存储在本地,因此在应用程序下次启动时可用,即使在飞行模式下也是如此.

我认为应用程序离线时进行的写入会在网络再次可用时与 FB 数据库同步。

看来完全不是这样。

[编辑]在飞行模式下写入已完成,但该数据似乎无法读取。

addListenerForSingleValueEvent 和 ValueEventListener 不检测没有网络的数据库写入。

谁能帮我理解 setPersistenceEnabled(true) 的作用,以及它的典型用例是什么?

编辑添加:

我编写了我能想到的最小的 Android Firebase 数据库应用程序。

这是应用类:

 import android.app.Application;
import android.util.Log;

import com.google.firebase.database.FirebaseDatabase;

public class TheApp extends Application {
@Override
public void onCreate() {
super.onCreate();
// See https://firebase.google.com/docs/reference/android/com/google/firebase/database/FirebaseDatabase.html#public-methods
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Log.d("TheApp", "application created");
}
}

所有主要 Activity 都是在 EditText 旁边显示一个“保存”按钮。在 onCreate MainActivity 中登录一个已知用户。当用户随后单击保存按钮时,EditText 中的字符串将写入 Firebase。

package com.grayraven.simpledb;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {
FirebaseAuth mAuth;
FirebaseUser mUser;
ValueEventListener mListener;
private FirebaseAuth.AuthStateListener mAuthListener;
private static final String TAG = "Main";
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference dbRef;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
dbRef = db.getReference("/data/strings/");

Button btnSave = (Button)findViewById(R.id.btn_save);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText = (EditText)findViewById(R.id.editText);
String text = String.valueOf(editText.getText());
dbRef.setValue(text);
}
});

signIn();

}

private void signIn() {
mAuth= FirebaseAuth.getInstance();
String email = "jim@example.com"; //valid credentials
String password = "12345678";
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());

// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
//always fails when offline
Log.w(TAG, "signInWithEmail failed", task.getException());
}
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

只要设备连接到网络,这一切都有效。

如果应用程序在没有网络连接的情况下启动,则登录失败,因此用户无能为力。

如果应用程序运行时网络连接断开,则不会保留任何已保存的字符串,并会在网络恢复时上传到 Firebase。

这是预期的行为吗?此行为似乎与下面引用的文档不一致:

public synchronized void setPersistenceEnabled (boolean isEnabled)

The Firebase Database client will cache synchronized data and keep track of all writes you've initiated while your application is running. It seamlessly handles intermittent network connections and re-sends write operations when the network connection is restored. However by default your write operations and cached data are only stored in-memory and will be lost when your app restarts. By setting this value to true, the data will be persisted to on-device (disk) storage and will thus be available again when the app is restarted (even when there is no network connectivity at that time). Note that this method must be called before creating your first Database reference and only needs to be called once per application.

最佳答案

我的问题很简单。即使我有我的 TheApp 类,我也忘记更新 list 以便首先调用 TheApp。您可以使用“名称”应用程序标签来做到这一点:

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name="com.grayraven.simpledb.TheApp"> <!-- I forgot this! -->

现在应用程序似乎在线或离线都可以正常工作,因为实际上正在调用 setPersistenceEnabled(true)。唯一的限制是,如果您注销然后丢失网络,则无法进行身份验证。但是看起来,如果您通过了身份验证,您的应用程序将可以离线工作,即使您重新启动它也是如此。至少看起来是这样。

关于android - FirebaseDatabase.getInstance().setPersistenceEnabled(true) 到底做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38001919/

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