android - Firebase 实时数据库首次连接花费的时间太长

标签 android performance firebase-realtime-database database-connection real-time

在这里,我们正在使用 Firebase Relatime 数据库 制作一个应用程序。当我们的应用第一次打开时,它会尝试连接到 firebase 数据库,这需要很多时间。在第一次连接之后,它真的很快。我想知道第一次如何快速建立连接?

最佳答案

在此我们可以使用 SERVICE 长时间运行的后台任务来解决第一次连接速度慢的问题。

/**
 * Created by bipin on 1/18/17.
 */

public class FirstConnectionService extends Service {


    public static DatabaseReference databaseReference;
    private static String TAG = FirstConnectionService.class.getSimpleName();


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        getDriverStatus();

        return START_STICKY;
    }


    /**
     *  long running background task using services
     */
    private void getDriverStatus() {

        try {

            databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(
                    Constants.FIREBASE_DRIVER + "/" + 100 + "/status");
            databaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    AppUtils.showLog(TAG, "onDataChange");

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                    AppUtils.showLog(TAG, "onCancelled");

                }
            });

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}

然后通过检查服务是否已在运行来启动服务

    /**
    * First check if service is already running.
    */
    if (!ServiceUtils.isMyServiceRunning(FirstConnectionService.class, this)) {

        AppUtils.showLog("FirstConnectionService", "isMyServiceRunning: false");
        startService(new Intent(this, FirstConnectionService.class));

    } else {

        AppUtils.showLog("FirstConnectionService", "isMyServiceRunning: true");

    }

服务实用程序

/**
 * Created by bipin on 1/18/17.
 */

public class ServiceUtils {

    /**
     * Check if service is already running in background
     * */
    public static boolean isMyServiceRunning(Class<?> serviceClass, Context context) {

        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {

            if (serviceClass.getName().equals(service.service.getClassName())) {

                return true;

            }
        }
        return false;
    }

}

关于android - Firebase 实时数据库首次连接花费的时间太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41711602/

相关文章:

java - 为什么将设备语言更改为 RTL 语言时 fragment/Activity 布局看起来很糟糕?

Android,我们可以使用 Parse LocalDataStore 进行本地存储吗?

c# - DataGridView 性能与 BindingList 数据源相结合

android - 将具有动态值的 Firebase 数据库转换为 Kotlin 数据类的最佳实践

ios - onDisconnectRemoveValue 不删除( key : value) after log out firebase?

android - Android 位置监控中的误报

android - 将 ApplicationContext 用于对话框是否安全?

Python Numpy 数据类型性能

performance - 与 Apache 相比,Node.js 的性能如何?

swift - 自动从 Firebase 数据库中删除数据