Search code examples
javaandroidfirebasegoogle-cloud-firestoresharedpreferences

Can I combine SharedPreferences and Firestore when saving data?


I'm a beginner developer and I'm currently developing a Quiz app. Each Quiz contains 50 questions and I'm storing them using Room database. I've got Coins, Stars, and the score and number of finished questions for each Quiz (And also user's information) that I want to save. And I still don't know what is the best method to do that.

I was thinking about combining SharedPreferences and Firestore to save user's progress and information, SharedPreferences to save them locally, and Firestore so that when the user signs in on another device, all his progress and information will be retrieved (and saved in SharedPref).

(**Data will be saved in Firestore when the user only signs in with Google)

Now my Questions is : Can I combine SharedPref and Firestore when saving data? and is it a good idea to do so? if not, should I only use Firestore for that?


Solution

  • Can I combine SharedPref and Firestore when saving data?

    Yes, you can but I cannot see any benefit at all. You say:

    Firestore so that when the user signs in on another device.

    To have the data available no matter what device the user is using means that each time the user closes the app, you should save the progress in Firestore. So I cannot see why would you use another data structure. Besides that, SharedPreferences do not persist along with app uninstalls. SharedPreferences data is always deleted.

    It would have been an excellent idea only if you wanted to store the progress in SharedPreferences and commit the quiz to Firestore only when it's finished. In this way, you'll only be billed with a single write operation, which sounds perfect. However, this solution doesn't provide the feature to have the progress available, no matter what the device the user uses, as it's stored only on a single device.