【问题标题】:App update deletes old shared prefs data from internal storage应用程序更新从内部存储中删除旧的共享首选项数据
【发布时间】:2018-09-22 09:59:05
【问题描述】:

在我的应用程序中,我有一个由用户生成的对象数据库,然后在用户离开应用程序时使用共享首选项保存到内部存储中。现在,当用户重新打开应用程序时,会检索该数据并将其呈现给用户以进行进一步编辑。 我注意到,当我向我的应用推出更新并且用户安装它时,所有数据都会丢失。我尝试通过使用共享首选项保存应用程序的当前版本代码来检索它,然后将其与当前版本代码进行比较以了解它何时是应用程序更新,然后我调用读取和写入数据方法来检索旧数据但没有运气. 关于我应该如何处理这个问题的任何想法?

序列化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!");
}

【问题讨论】:

    标签: java android sharedpreferences updates


    【解决方案1】:

    您的 SerializeGLBData.Write 函数是如何工作的?因为通过阅读您的代码,当您在升级的情况下,您只是直接调用CallWriteDataMethods,并根据您在其中的评论:

    // 将 GlobalDataHolder 中的所有 -empty- 数据写入内存,避免第一次读取错误

    您正在用空数据写入内存。您的编写函数是否在将空数据放入其中之前检查数据是否存在?

    类似

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

    【讨论】:

    • 刚刚添加了 SerializeGLBData 文件 :D 。只有在新安装时,对 CallWriteDataMethods 的调用才会将空数据写入内部存储,这反过来意味着用户不会向他们的数据库添加任何内容。
    • 编辑:@Macmist 是对的,我只是在内部存储上写入空数据。我所要做的就是读取以前保存的数据,然后一切都很好。谢谢
    猜你喜欢
    • 2011-10-27
    • 1970-01-01
    • 2019-02-18
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 2015-10-17
    相关资源
    最近更新 更多