Firebase数据库连接每秒都会反复连接和断开,直到应用程序未关闭。由于这种波动:
代码已经在以下设备中进行了测试,并且在两个设备中都出现了问题: -
模拟器
Google Play服务10.0.84
Android版6.0
dependencies{
compile 'com.google.firebase:firebase-database:10.0.1'
compile 'com.google.firebase:firebase-auth:10.0.1'
}
三星Galaxy J2
Google Play服务10.2.97
Android版本5.1.1
dependencies{
compile 'com.google.firebase:firebase-database:10.0.1'
compile 'com.google.firebase:firebase-auth:10.0.1'
}
重现问题的步骤:
stackoverflow链接,我认为与此问题有关
Firebase .info/connected showing wrong connection states
产生问题的代码是
package com.app.sample;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
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.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class TestActivity extends AppCompatActivity {
private final String TAG = "TestActivity";
Handler handler = new Handler();
private ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
log(connected ? "Online" : "Offline");
}
@Override
public void onCancelled(DatabaseError databaseError) {
log(databaseError.getDetails());
}
};
private DatabaseReference connectedRef;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user == null) {
signIn();
} else {
log("User is already logged in");
}
connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(valueEventListener);
}
private void signIn() {
FirebaseAuth.getInstance().signInAnonymously()
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
log("Signed in successfully User id " + user.getUid());
} else {
log("Unable to authenticate app." + task.getException());
}
}
});
}
@Override
protected void onResume() {
super.onResume();
FirebaseDatabase.getInstance().goOnline();
}
@Override
protected void onStop() {
super.onStop();
FirebaseDatabase.getInstance().goOffline();
}
private void log(String msg) {
Log.e(TAG, msg);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (connectedRef != null) {
connectedRef.removeEventListener(valueEventListener);
}
}
}
在logcat下面重复日志,直到应用程序打开。
02-07 09:19:16.677 2435-2635/com.google.android.gms I/AuthChimeraService: Executing send connection operation 02-07 09:19:18.185 4633-4660/com.fire.base.test D/EventRaiser: Raising 1 event(s) 02-07 09:19:18.186 4633-4633/com.fire.base.test D/EventRaiser: Raising /.info/connected: VALUE: true 02-07 09:19:18.186 4633-4633/com.fire.base.test E/TestActivity: Online 02-07 09:19:18.588 4633-4660/com.fire.base.test D/EventRaiser: Raising 1 event(s) 02-07 09:19:18.588 4633-4633/com.fire.base.test D/EventRaiser: Raising /.info/connected: VALUE: false 02-07 09:19:18.588 4633-4633/com.fire.base.test E/TestActivity: Offline 02-07 09:19:19.686 2435-2691/com.google.android.gms E/AsyncOperation: serviceID=16, operation=ValidateAuthServiceOperation java.lang.NullPointerException: onPostInitComplete can be called only once per call to getRemoteService at kpd.a(:com.google.android.gms:74) at kme.a(:com.google.android.gms:987) at kna.a(:com.google.android.gms:66) at kvb.a(:com.google.android.gms:284) at ekn.a(:com.google.android.gms:125) at ekn.a(:com.google.android.gms:113) at kvi.run(:com.google.android.gms:113) at kyl.run(:com.google.android.gms:450) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at lco.run(:com.google.android.gms:17) at java.lang.Thread.run(Thread.java:818)