- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我似乎不理解 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/
所以,我是 Firebase 的新手,我正在关注这个人关于如何将 Swift 应用连接到 Firebase 的教程,这对他来说完全没问题。但是当我尝试导入“FirebaseDatabase”时,它给了
friend 们,我安装名为用于选择国家/地区的 SwiftCountrySelector,但在安装 pod 后我收到名为 FirebaseDatabase/FirebaseDatabase.h 的错
如何在适用于 Android 的新 FirebaseDatabase API 中设置 FirebaseDatabase 的缓存大小? 我只是将我的遗留代码更新到新版本,旧设置是: Fireba
我看到了 databaseReference 和 FirebaseDatabase 之间的一些差异,例如:databaseReference 用于所有数据库操作,但我想了解更多主要差异。 最佳答案 这
我正在使用带有 Firebase 数据库的 Swift 3 开发 iOS 应用。 我的播客文件: target 'MyApp' do # Comment the next line if you'
我正在将数据从 js.function 发送到 Firebase 数据库。数据由两个字符串和1个数据/时间变量组成,js函数的源代码如下: $('#send_button').click(functi
我想知道是否可以使用 FirebaseDatabase 方法为全局变量赋值。我知道 FirebaseDatabase 方法是异步的,并且在后台执行,不会阻塞主线程。但我陷入了在 onDataChang
我有一个问题,我不能只得到最近添加的 child 。 这是我的数据库: 这是我的测试代码: val db = FirebaseDatabase.getInstance().reference
removeAllObservers() 是否仅删除其调用的 DatabaseReference 实例的所有观察者,或指定路径上的任何引用? 例如: MainController.swift let
我正在尝试使用简单的搜索栏 (EditText) 按用户名搜索存储在我的 firebase 数据库中的用户,并在单击按钮时显示该特定用户 我的 findfriends.java 文件中有以下代码 pu
这个问题在这里已经有了答案: how to stop the items duplication in recyclerView android (2 个答案) How do I clear an
我使用 firebase 数据库,在我的应用程序中我将一些数据放入数据库中,但问题是我没有检索这些数据。使用此代码,我希望使用唯一的用户名进行注册,但是当我将数据放入注册 Activity 时,具有相
我正在从 firebase 数据库中检索所有用户并将它们显示在 recyclerView 中。它显示城镇、用户名和图像。我能够检索,但它只显示一个用户而不是所有用户。数据库有很多用户,但只显示一个。任
我正在制作一个应用程序,在该应用程序中,用户登录,我存储他们的信息;但是,我注意到在他们注册后我没有用户的密码信息。在通过 Firebase 注册时存储用户密码是个好主意吗?有什么地方我需要他们的密码
我正在尝试使用 Flutter 在 firebase 数据库中运行事务,但出现错误,有人可以帮我解决这个问题吗? 代码: ref.runTransaction((MutableData transac
这个问题已经有答案了: Should FirebaseDatabase.getInstance() be used sparingly? (1 个回答) 已关闭 6 年前。 使用 Firebase 开
我正在尝试访问 shrubs 子项,并在 shrubs 下的每个唯一键下访问键值对,例如键:ShrubbedWord 值:yfyj 我已经尝试过,但我什么也没得到:- postsData = Fire
我希望我的 Firebase 数据库使用用户个人资料图片 URL 进行初始化我尝试添加此内容,但我认为 profileImageUrl 变量未使用 ImageUri 变量进行初始化?请帮助完成这件事.
所以我目前正在开发像snapchat这样的克隆,我正在向服务器发送拉取请求,但至于下载,进展并不顺利。我创建了一个对数据库的引用,如下所示, var recievers: FIRDatabaseRef
想要将此代码连接到 FirebaseDatabase。但我在连接时遇到错误。 错误:- 无法将“[Datasnapshot]”类型的值转换为预期的参数类型“UView”。 Below is Code
我是一名优秀的程序员,十分优秀!