用户更新应用程序时如何更新共享偏好数据? [英] How to update Shared Prefrences data when user updates the app?

查看:34
本文介绍了用户更新应用程序时如何更新共享偏好数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道假设我在 android 应用程序中存储了一些东西,例如 - 在共享首选项中并假设应用程序的当前版本是 n 和更新版本 (n + 1),我将共享首选项对象的数据类型从 List 更改为 List,并且在下一个更新版本中是 (n+2) 我已经将对象的数据类型更新为 List 现在当用户去商店更新他/她的应用程序时,以前的数据将是在那里,所以我必须自己编写一些代码,如果以前的版本是 n 而当前版本是例如 - (n+2) 现在我将更改通过编写 if-else 来检查旧版本.

I was just wondering that as suppose I am storing something in android app e.g - in Shared Preferences and suppose current version of app is n and in the updated version (n + 1), I changed the data type of my shared preference object from List<String> to List<Object>, and in the next updated version which is (n+2) I have updated the data type of object to List<Double> now when user will go to play store update his/her app, then the previous data will be there so I would have to write some code by myself that if previous version is n and current version is e.g - (n+2) now I will change the value in the Shared Prefrence in the code accordingly by writing the if-else to check what is the older version.

开发人员在 Playstore 上更新任何 Android 应用程序时使用的方法是否高于您的方法?

Is above the approach you developers use when you update any android app on the playstore?

还有任何可能性,例如用户当前安装了我的应用程序的 (n+2) 版本,现在用户希望下载我的应用程序的任何旧版本,例如版本 n,现在用户是否有可能安装此版本 n 应用程序,同时还可以为版本 (n+2) 恢复数据,因为如果然后会发生在我的版本 n 代码中,将没有 if-else 将对象的数据类型从 List 更改为List ?

Also Is there any possiblity like for example user has (n+2) version of my app currently installed and now user wish to download any older version of my app e.g version n, now Is there any possibility that user will be able to install this version n app while also having data restored for version (n+2), because if that will happen then as in my version n code, there will be no if-else to change data type of object from List<double> to List<String> ?

我是一个新手,从来没有在 Playstore 上放过任何应用,但我会去,这就是为什么要问.

I am a newbie, have never put any app on playstore but going to, that's why asking.

谢谢

推荐答案

  1. 不要担心降级方案,因为 Google Play 不支持它.即使用户手动尝试降级(即您直接将 .apk 共享给用户),共享首选项也将被清除.

  1. Don't worry about the downgrade scenario, since Google Play doesn't support it. And even if the user manually tries to downgrade (i.e you sharing the user the .apk directly), the shared preferences will be cleared out anyway.

共享首选项没有迁移逻辑,就像我们对数据库的方式一样.所以这必须手动完成.

There is no migration logic for shared preferences, like the way we have for Databases. So this has to be done manually.

我的建议是使用不同的键,而不是通过 if else.始终这样做,因为我更喜欢保持代码干净,而不是共享首选项文件.

My suggestion is to use a different key instead of going through the if else. Do this always, since I prefer keeping my code clean, rather than the shared preferences file.

这篇关于用户更新应用程序时如何更新共享偏好数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆