Firestore - Checking The Connection Status Of The Module To The Server
我已经注意到,在关闭Internet连接并重新打开之后(无论我的
这是正常现象吗?
如果可以,我可以以某种方式检查
据我所知,Cloud Firestore中没有等效于Firebase Realtime Database的
The Realtime Database has native support for presence -- that is, being able to tell when a user has come online or gone offline. While we do have a solution for Cloud Firestore, it's not quite as elegant.
如果您阅读Firestore官方文档,则将通过结合使用Realtime Database和Firestore来实现在线状态系统。
我目前正在为此目的测试修复程序。
第1步:使用以下命令确保设备已建立数据连接
Android推荐的方法
1 2 3 4 5 6 7 | public static boolean isConnectedToInternet(@NonNull Context _context) { ConnectivityManager cm = (ConnectivityManager)_context.getSystemService(Context.CONNECTIVITY_SERVICE); if (cm == null) return false; NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); } |
第2步:强制Firestore连接(其想法是覆盖Firestore的内部连接重试指数补偿)
1 | FirebaseFirestore.getInstance().enableNetwork() |
第3步(可选):如果在后台运行应用程序时执行此操作,则可能还需要使用Android的Task API来同步等待,直到enableNetwork()任务完成。否则,操作系统可能会认为您的后台工作已经完成,并切断了您的应用对系统资源(网络,CPU等)的访问。
1 2 3 4 5 | try { Tasks.await(FirebaseFirestore.getInstance().enableNetwork()); } catch (Exception e) { Log.e(TAG,"error in Firestore enableNetwork():", e); return; } |