简体   繁体   English

应用更新从内部存储中删除旧的共享首选项数据

[英]App update deletes old shared prefs data from internal storage

In my app, I have a database of objects that are generated by the user and then saved to the internal storage using shared prefs when the user leaves the app.在我的应用程序中,我有一个由用户生成的对象数据库,然后在用户离开应用程序时使用共享首选项保存到内部存储中。 Now when the user re-opens the app, that data is retrieved and presented to the user for further editing.现在,当用户重新打开应用程序时,该数据将被检索并呈现给用户以供进一步编辑。 I noticed that when I roll out an update to my app and the user installs it, all the data is lost.我注意到当我对我的应用程序推出更新并且用户安装它时,所有数据都丢失了。 I tried retrieving it by saving the app's current version code using shared prefs and then comparing it to the current one in order to know when it's an app update and then i call the Read & Write data methods to retrieve the old data but with no luck.我尝试通过使用共享首选项保存应用程序的当前版本代码来检索它,然后将其与当前版本代码进行比较以了解它何时是应用程序更新,然后我调用读取和写入数据方法来检索旧数据但没有运气. Any ideas on how i should approach this issue?关于我应该如何解决这个问题的任何想法?

SerializeGLB.java:序列化GLB.java:

public class SerializeGLBData {

/**
 * Writes the Global User Box's cardList to the user's internal storage using the Gson
 * library so that the user doesn't lose his/her data.
 * @param cardList The list to write to the internal storage
 * @param context Getting the app's current context
 */
public static void Write(ArrayList<Card> cardList, Context context) {

    SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = appPrefs.edit();
    Gson gson = new Gson();
    String cardsGLBJson = gson.toJson(cardList);
    editor.putString("cardsGLB",cardsGLBJson);
    editor.apply();
    editor.commit();
    Log.d("WriteData","Data written successfully!");
}

/**
 * Reads the cards list that gets saved when the app closes
 * @param context Get the app's current context
 * @return Returns an ArrayList of Card Objects containing the card info
 */
public static ArrayList<Card> ReadCards(Context context) {
    SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    Gson gson = new Gson();
    String cardsGLBJson = appPrefs.getString("cardsGLB","");
    Type type = new TypeToken<ArrayList<Card>>(){}.getType();
    return gson.fromJson(cardsGLBJson,type);
}

} }

 private void checkForFirstRun() {

    final String PREF_VERSION_CODE_KEY = "version_code";
    final int DOESNT_EXIST = -1;

    // Get current version code
    int currentVersionCode = BuildConfig.VERSION_CODE;

    // Get saved version code
    SharedPreferences prefs = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
    int savedVersionCode = prefs.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST);

    // Check for first run or upgrade
    if(currentVersionCode == savedVersionCode) {
        // This is just a normal run
        Log.d("RUN_TYPE:" , "Normal Run");
    } else if(savedVersionCode == DOESNT_EXIST) { // This is a new install(or the user cleared the shared prefs)
        CallWriteDataMethods(this);
        Log.d("RUN_TYPE:", "New Install");
        // Showing the tutorial page when the app starts for the first time
        Intent tutorialIntent = new Intent(this, Tutorial.class);
        startActivity(tutorialIntent);
        UsernameDialog dialog = new UsernameDialog();
        dialog.setCancelable(false);
        dialog.show(getFragmentManager(),"USERNAME_DIALOG");
    } else if(currentVersionCode > savedVersionCode) { // This is an upgrade
        CallWriteDataMethods(this);
        Log.d("RUN_TYPE:","Update");
    }

    // Update the shared prefs with the current version code
    prefs.edit().putInt(PREF_VERSION_CODE_KEY,currentVersionCode).apply();
    return;
}

 public static void CallWriteDataMethods(Context context) {
    // Write all the -empty- data from GlobalDataHolder to the internal memory to avoid a first time read error
    SerializeGLBData.Write(GlobalDataHolder.cards,context);
    // Write all the -empty- data from JPDataHolder to the internal memory to avoid a first time read error
    SerializeJPData.Write(JPDataHolder.cards,context);
}

 /**
 * Calls every available Read method to retrieve all available data from the GLB database
 */
public static void callReadDataMethodsGLB(Context context) {
    GlobalDataHolder.cards = SerializeGLBData.ReadCards(context);
    Log.i("Read Methods[GLB]", "ReadMethods called!");
}

/**
 * Calls every available Read method to retrieve all available data from the JP database
 */
public static void callReadDataMethodsJP(Context context) {
    JPDataHolder.cards = SerializeJPData.ReadCards(context);
    Log.i("Read Methods[JP]", "ReadMethods called!");
}

How does your SerializeGLBData.Write function works?您的 SerializeGLBData.Write 函数如何工作? Because by reading your code, when you are in the case of an upgrade, you are only calling the CallWriteDataMethods directly, and according to your comment in it:因为通过阅读您的代码,当您在升级的情况下,您只是直接调用 CallWriteDataMethods,并根据您在其中的注释:

// Write all the -empty- data from GlobalDataHolder to the internal memory to avoid a first time read error // 将 GlobalDataHolder 中的所有 -empty- 数据写入内部存储器以避免首次读取错误

You are writing the memory with empty data.您正在用空数据写入内存。 Are your writing functions checking if data exists before putting empty data in it?您的编写函数是否在将空数据放入之前检查数据是否存在?

So something like所以像

if(!prefs.contains("your_data_key")) {
   // your code to add data
}

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

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