0

I am working on android multiplayer game. My JSON file contains 800 000+ dictionary words. JSON array is being loaded in List once players enter GameActivity to actually play game. So each time player starts 1vs1 game, JSON is being loaded into a List<>.

Now, I have 2 options:

  1. Load JSON the most optimal way so user does not get out of memory error after a while

  2. Load JSON file into a List<> only once, when application is open.

I'm not sure about any of these 2 cases, can someone give me a hand on how to do this please? Thank you

1 Answers1

0

I can't comment because of my repu. But you can try to few different approaches into this problem.

If your JSON changes very rarely i can suggest this method.

You can put your json inside your asset file. and call in runtime with this

public String loadJSONFromAsset() {
    String json = null;
    try {
        InputStream is = getActivity().getAssets().open("yourfilename.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

Or you can fetch your Json before app starts and store in ContentProvider (DB or SharedPreference). And this way you only fetch your json when it is necessary.

//right before fetching JSON
boolean thereIsAnyUpdatesInJSON = checkYourDictionaryJsonIfThereAreAnyUpdates();
if(!isDictionaryDatabaseAvailable && thereIsAnyUpdatesInJSON){
     fetchYourDictJson();
}
Ferhat Ergün
  • 135
  • 1
  • 10
  • I'm using exact same fetch method. I have tried to save List into sharedPreference, however, it's to large, I get an error. Dictionary has 800 000+ words. adding it to intent.putExtraArraylist..("x", arraylist); does not work too. – Marko Marović May 09 '18 at 11:49
  • Then try to store inside DB but you might need to split your dictionary for faster execution (like A-D,E-G etc...or something else if it wont fit your situation) – Ferhat Ergün May 09 '18 at 12:00