簡體   English   中英

Firebase 不會在真實設備上檢索數據,但在模擬器上可以正常工作

[英]Firebase won't retrieve data on real device, but works normally on emulator

我正在開發一個帶有 firebase 用於存儲的公共汽車跟蹤應用程序。

我目前檢索電台列表的方法似乎在模擬器中運行良好

該方法需要在 firebase 中獲取我的 Stations 集合中的所有文檔

Firebase 數據庫集合

方法

getAllBusStops()

  private void getAllBusStops()
    {


        FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
                .setTimestampsInSnapshotsEnabled(true)
                .build();
        mDb.setFirestoreSettings(settings);

        CollectionReference busStopRef = mDb
                .collection(getString(R.string.collection_bus_stops));

        
        busStopRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
            ArrayList<BusStop> tempBusStops = new ArrayList<>();
                int i = 0;
                for (QueryDocumentSnapshot doc : task.getResult())
                {
                    BusStop busStop = doc.toObject(BusStop.class);

                    
                        GeoPoint getGeoFirebase = (GeoPoint) doc.get("position");
                        busStop.setPosition(getGeoFirebase);
                        tempBusStops.add(busStop);
                    
                    
                }

                dataViewModel.getmBusStopsLiveData().setValue(tempBusStops);
            }
        });

    
    }

調用getAllBusStops的位置

@Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getAllBusStops();
    }

在 api 27 上運行的模擬器上的結果

模擬器

調試顯示可以從firebase取回數據

調試結果

問題:在真實設備上沒有檢索到快照

我嘗試連接我的真實設備進行調試,但是用於獲取 Stations 集合getAllBusStops()的 firebase function 似乎沒有返回任何快照,我嘗試調試並且來自task.getResult()的快照數量為 0,在模擬器設備上的數量檢索到的快照數量為 3,與我的 firebase 數據庫中的文檔數量完全相同

總之,這行代碼在調用onComplete事件時可以在模擬器上正常獲取數據

for (QueryDocumentSnapshot doc : task.getResult())

但在真實設備上有 0 個快照

我讀過一些帖子 firebase 需要設置權限,所有權限要求都已在我的項目清單中設置

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

真機已授予所有權限,但仍然沒有數據我真的不明白發生了什么,我該如何解決這個問題?

  1. 你的真實設備是什么?
  2. 您是否嘗試過卸載應用程序/重建項目?
  3. 你在使用 Firebase 身份驗證嗎?
  4. 向我們展示您的 Firestore 安全規則

問題已解決

我追溯了日志,顯然互聯網連接太弱而無法訪問 Firestore 后端

錯誤日志:

W/Firestore: (21.5.0) [OnlineStateTracker]: Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds
    
    This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

我切換到更強的 wifi 並且應用程序正在運行 好的,這有點奇怪,因為我的 4G 連接的平均速度為 4 MB/S。 當我 go 沿着

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM