在每个会话中将用户数据保存在共享首选项中,或者从 Firebase 检索数据。

huangapple go评论69阅读模式
英文:

Saving User datas in Shared Prefererences or Retrieving datas from firebase in every session

问题

我正在使用 Android Studio 和 Firebase 为我的学校开发一个基于角色的应用程序。我无法决定如何显示或控制用户数据。

我正在使用 Firestore 来存储用户数据,例如:姓名、角色、班级。
当用户启动活动时,该活动应该从自己的文档中获取用户数据,对吗?

但我不希望在每次部署应用程序时都读取数据。因此,我使用了 Shared Preferences 来存储数据。然而,这使代码变得复杂并降低了可重用性。
而且,由于我的应用程序是基于角色的,从 Shared Preferences 中很难检查用户是否是管理员。

在每次部署应用程序时读取用户文档

优点:除了账单之外,一切都看起来不错。

缺点:读取操作的费用。

存储在 Shared Preferences 中

优点:看起来经济实惠。

缺点:代码复杂,可重用性较低,if else 语句数量较多。


哪种方法更好?

或者您有任何建议吗?

英文:

I am developing a role-based application for my school using android studio with firebase. I couldn't make a decision about how can i display or control user datas.

I am using firestore for storing user datas, for example; name,role,class.
When user starts activity, the activity should fetch user datas from it's own document, right?

But i didn't want to read datas in every deploy of application. So i used shared preferences for storing datas. However it complexed code and reduced reusability.
Also since my application is role-based, it's hard to check if user is admin from shared preferences

Reading user document in every deploy of application

pros: Everything seems fine except bill

cons: Bills of read operations

Storing in shared preferences

pros: Seems economic

const: complex code, reusability is low, if else count is high


Which one is better?

Or do you have any suggestions?

答案1

得分: 1

一个数据库和本地存储有两个不同的用途。既然您说您需要检查用户是否为管理员,您可能需要一个位于互联网上的数据库来验证,而不是将其存储在本地设备上。

如果您不需要验证任何内容,也不需要在设备之间/用户之间共享数据,您可以使用本地存储。但是,应该使用共享首选项仅存储小型数据,即键值对数据。如果您想存储更复杂的数据和更多的数据,可以使用SQLite(https://developer.android.com/training/data-storage/room)。

至于计费问题,如果价格是一个问题,您总是可以自己设置一个带有PostgreSQL数据库的HTTP服务器,例如。

英文:

A database and local storage have 2 separate use cases. Since you say that you need to check if an user is admin, you will probably need a database on the internet to validate that yourself, not store it on the local device.

If you don't need to validate anything or share data between devices/users or whatever, you can use local storage. But shared preferences should be used to store small data, key-value data only. If you want to store more complex data and more data in general, use sqlite(https://developer.android.com/training/data-storage/room).

As for the billing, if price is an issue you can always setup your own http server with a postgresql database for example.

huangapple
  • 本文由 发表于 2020年10月9日 00:58:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64267276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定