Firebase数据库连接重复连接和断开连接(每秒一次)

时间:2017-02-05 00:29:19

标签: firebase firebase-realtime-database

  • Firebase数据库连接每秒都会反复连接和断开,直到应用程序未关闭。由于这种波动:

    1. FirebaseDatabase无法从firebase服务器保存和接收数据。
    2. ValueEventListener未调用。

  • 代码已经在以下设备中进行了测试,并且在两个设备中都出现了问题: -

    1. 模拟器

      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' }

    2. 三星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' }

  • 重现问题的步骤:

    1. 将设备时钟时间设置为当前时间。
    2. 执行TestActivity.java
      当此活动第一次执行时,用户将使用FirebaseAuth匿名登录,等待几秒钟用户将成功登录。现在转到第3步。
    3. 按下关闭应用程序。
    4. 现在将设备时钟时间增加2天或更多。
    5. 再次打开应用程序并检查logcat。问题将立即发生或在几分钟后或有时甚至在1或2小时后发生。如果没有立即出现问题,请关闭应用程序并在几分钟或几小时后打开它(当设备闲置2-3小时后,肯定会出现问题);
  • 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)

0 个答案:

没有答案