質問

I have an Android app that loads a remote JSON feed every 5 seconds. The payload of the JSON file is about 240kb. I would also like to cache this data so that users can view the last loaded feed when they are offline.

Is it advisable to write the raw data to the internal storage every time the feed is fetched?

役に立ちましたか?

解決

I wouldn't. I'd memory cache it, and write the last version to the file system in onPause(). That way you don't constantly write to disk.

他のヒント

I agree with Gabe. Save the data in the onPause() Method. You can never be certain when the system or user will terminate your app so apply the onPause() Method to every activity in your app, then call a class or static method that will perform the save to the apps cache directory.

Best of Luck.

Why don't you use SharedPreferences?

SharedPreferences sharedPref = getSharedPreferences("Test", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("jsondata", jobj.toString());

...

String strJson = sharedPref.getString("jsondata");
if(strJson != null) JSONObject jsonData = new JSONObject(strJson);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top