简体   繁体   中英

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?

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.

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.

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. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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