简体   繁体   English

Firebase Firestore在应用启动时检索数据,而不是保存数据

[英]Firebase Firestore retrieving data on app start instead of saving it

I got an issue on my Android app that makes it get information from the Firestore Database every time the application is loaded. 我的Android应用程序出现问题,使该应用程序每次加载时都从Firestore数据库获取信息。 In the Firebase Docs, they say that persistence is enabled by default, but that seems not to work for me. 在Firebase Docs中,他们说默认情况下启用了持久性,但这似乎对我不起作用。 Tried to add the settings but nothing changed. 尝试添加设置,但未更改。

FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
            .setPersistenceEnabled(true)
            .build();
    db.setFirestoreSettings(settings);

The query inside onCreate: onCreate中的查询:

db.collection("administradores").whereEqualTo("email", user.getEmail()).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (DocumentSnapshot document : task.getResult()) {
                                tipo = (long) document.getData().get("tipo");
                                if (tipo == HomeFragment.EVENTO_CODE){
                                    menu.findItem(R.id.nav_nuevo).setTitle("Nuevo evento");
                                    menu.findItem(R.id.nav_editar).setTitle("Tus eventos");
                                    fragment.tipoEmpresa.setText("Administrador de eventos");
                                    fragment.tusEvLoc.setText("Tus eventos");
                                }else if(tipo == HomeFragment.COMERCIO_CODE){
                                    menu.findItem(R.id.nav_nuevo).setTitle("Nuevo local");
                                    menu.findItem(R.id.nav_editar).setTitle("Tus tiendas");
                                }else if(tipo == HomeFragment.BAR_CODE){
                                    menu.findItem(R.id.nav_nuevo).setTitle("Nuevo local");
                                    menu.findItem(R.id.nav_editar).setTitle("Tus establecimientos");
                                }
                            }
                        }
                    }
                });

I am expecting the data to be saved (downloaded) only once, and be updated when there are changes in the database. 我希望数据仅保存(下载)一次,并且在数据库中有更改时进行更新。 May this work with the Firestore tools or is it necessary to create an internal database (ie SQLite) to save all the data? 这可以与Firestore工具一起使用,还是必须创建一个内部数据库(即SQLite)来保存所有数据?

use this to get data from cache. 使用它从缓存中获取数据。 if you want to manually save the data you would need to store it in a local database and work out a way to keep the data synced in your local database. 如果要手动保存数据,则需要将其存储在本地数据库中,并找到一种使数据在本地数据库中保持同步的方法。 the addSnapshotListener runs when any document is added modified or removed in the collection. 当添加或修改集合中的任何文档时,addSnapshotListener都会运行。

db.collection("cities").whereEqualTo("state", "CA")
    .addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot querySnapshot,
                            @Nullable FirebaseFirestoreException e) {
            if (e != null) {
                Log.w(TAG, "Listen error", e);
                return;
            }

            for (DocumentChange change : querySnapshot.getDocumentChanges()) {
                if (change.getType() == Type.ADDED) {
                    Log.d(TAG, "New city:" + change.getDocument().getData());
                }

                String source = querySnapshot.getMetadata().isFromCache() ?
                        "local cache" : "server";
                Log.d(TAG, "Data fetched from " + source);
            }

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM