0

I'm using 4 RecycleView in MainActivity which load data from Cloud Firestor using FirestoreRecyclerAdapter. Now I want to use Room to create receipt for the user in ReceiptActivity.

I did all staff, but there is something wrong when I try to delete and update item in Room. My app is crashing, so I'm asking if there is conflict with what I'm doing or not? If not,please guide me with best practice to do that. Thanks in advance.

Error message

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
beso
  • 9
  • 4
  • Why are you using Room, for caching? – Alex Mamo Feb 09 '19 at 09:29
  • I'm using Room to save all items user decided to buy and show it in ReceiptActivity (he can change count of item or delete it from MainActivity and ReceiptActivity),that's what i'm looking for, if there is another way to do that tell me (I'm a beginner) – beso Feb 09 '19 at 10:24

1 Answers1

0

When using Cloud Firestore, offline persistence:

For Android and iOS, offline persistence is enabled by default. To disable persistence, set the PersistenceEnabled option to false.

Which means that Firestore will create a local (internal) copy of your entire database on users device. So the same thing that you want to get can be also achieved without adding an extra local database because by default is one that already exists.

Furthermore, if you need to get the data from the cache only, you can achieve this with the help of the DocumentReference.get(Source source) and Query.get(Source source) methods.

By default, get() attempts to provide up-to-date data when possible by waiting for data from the server, but it may return cached data or fail if you are offline and the server cannot be reached. This behavior can be altered via the Source parameter.

So we can now pass as an argument to the DocumentReference or to the Query the source so we can force the retrieval of data from the server only, chache only or attempt server and fall back to the cache.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • do you mean that on (buy click) I will create a document that store list of items then load it in ReceiptActivity from cach ? and for update count of item or delete it will work without using Room ? if that you meant so I need sub collection inside user document to hold that and a top level collection that hold orders when Order button clicked, am I right? – beso Feb 09 '19 at 10:51
  • Yes, you can load them from cache. Yes, it will work without it. For counting documents please see my answer from this **[post](https://stackoverflow.com/questions/48534676/get-collectionreference-count/48540276)**. Yes, that is an option. – Alex Mamo Feb 09 '19 at 11:32
  • thanks Alex Mamo ,your answer is so clear and good one,but I was thinking that it will cost me more in firebase if every user create a document without buy, so is there a way to use Room or something else? – beso Feb 09 '19 at 11:47
  • Once a document is cached, all the future request are from the cache, as long as that document isn't changed. You can implement Room if this what you want. On how to implement it, I recommend you see the [oficial documentation](https://developer.android.com/topic/libraries/architecture/room). It's very well explained. – Alex Mamo Feb 09 '19 at 12:04