应用被杀死后,如何保持从Firebase提取的数据?

时间:2019-01-17 19:28:15

标签: android firebase firebase-realtime-database

我的应用正在这样获取数据。如果应用程序被杀死,我想保留从Firebase获取的数据,并在再次创建应用程序时使用它,因此,我只想在数据库更改而不是onViewCreated时才获取数据。我认为我应该使用房间数据库。首先从房间数据库中获取数据,然后显示房间数据库中的数据。如果用户输入新数据,则值事件侦听器将工作并将数据再次提取到会议室数据库。这是个好方法吗?如果没有的话,有什么好方法吗?

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    this.v = view;
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);

    final RecyclerView productListView = v.findViewById(R.id.productList);

    final ProgressBar mProgresBar = v.findViewById(R.id.inventoryProgressBar);

    getDatabase();

    fetchProductsToRecyclerList(getContext(),productListView,mProgresBar);}

例如,我正在使用值事件侦听器,并且我希望该应用仅使用此事件侦听器来获取数据。

public void fetchProductsToRecyclerList(final Context context, final RecyclerView rv,
                                               final ProgressBar pb) {
    DatabaseReference myRef = getFirebaseRef();
    String userID = getUserId();
    myRef.child(userID).child("Products").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            products = new ArrayList<>();
            pb.setVisibility(View.VISIBLE);
            int counter = 0;
            //Get all of the children at this level
            Iterable<DataSnapshot> children = dataSnapshot.getChildren();
            for (DataSnapshot child : children) {
                Iterable<DataSnapshot> cocuklar = child.getChildren();
                for (DataSnapshot cocuk : cocuklar) {
                    counter += 1;
                    Product prd = cocuk.getValue(Product.class);
                    if (!prd.getProductID().equals("0x0x00x0xx"))
                        products.add(prd);

                }
            }
            mAdapter = new ProductAdapter(context, products);

            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
            DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(rv.getContext(),
                    ((LinearLayoutManager) layoutManager).getOrientation());
            rv.addItemDecoration(dividerItemDecoration);
            rv.setHasFixedSize(true);
            rv.setLayoutManager(layoutManager);
            rv.setItemAnimator(new DefaultItemAnimator());
            rv.setAdapter(mAdapter);
            if (counter == 0) {
                //    emptyList.setVisibility(View.VISIBLE);
            } else {
                //    emptyList.setVisibility(View.INVISIBLE);
            }
            pb.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

1 个答案:

答案 0 :(得分:0)

默认情况下,您使用Realtime Database SDK查询的任何数据都将缓存在本地存储中。只要服务器上的数据是最新的,该高速缓存将用于后续查询。您无需执行任何操作即可启用此功能。您不需要额外的缓存,除非您需要以不同于SDK提供的某种方式查询数据。

Read about disk persistence in the documentation.

相关问题