简体   繁体   English

UserDefaults / SharedPreferences还是DataBase更好地保存视图状态?

[英]UserDefaults/SharedPreferences or DataBase is better to save view state?

I have some scens with and I want save their state of view when app is closed. 我有一些场景,当应用程序关闭时,我想保存它们的视图状态。 Some times it's one thing like "is that scene has opened shop window?" 有时候是一回事,例如“那个场景是否已打开商店橱窗?” but sometimes ther is much more info which I need to store. 但有时还有更多我需要存储的信息。 So is better to use for it UserDefaults/SharedPreferences (json to string) or create model, serialize those info to model then save it to DB? 因此,最好使用UserDefaults / SharedPreferences(将json转换为字符串)或创建模型,将这些信息序列化为模型,然后将其保存到DB?

I will be thankful for Your opinion. 感谢您的意见。

I use SharedPreferences when i have to save a variable, such as UserName, Country of Origin, access token, location, UsersCurrentLevel, UsersCurrentHealth etc. 我必须保存变量时使用SharedPreferences,例如用户名,原产国,访问令牌,位置,UsersCurrentLevel,UsersCurrentHealth等。

I use Database when i have data that requires much manipulation or has more specifications to it. 当我有需要大量操作或具有更多规范的数据时,便使用数据库。 Such as questions for users, video ad's details(seenState , directoryPath, urlToDownloadVideo, hasVideoBeenAlreadyDownloaded etc.), lists of data, etc. 例如用户问题,视频广告的详细信息(seenState,directoryPath,urlToDownloadVideo,hasVideoBeenAlreadyDownloaded等),数据列表等。

So i would suggest you use a mix depending on the data.if it has a single entry use SharedPreference else if it has a list/ multiple data for a single format put it in a DB. 所以我建议您根据数据使用混合。如果它具有单个条目,请使用SharedPreference否则,如果它具有单个格式的列表/多个数据,则将其放入数据库中。 Hope this helps. 希望这可以帮助。

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

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