0

I have an android app where I extend the application object as such

public class Globals extends Application {
    private Map<String, Object> Creators = new LinkedHashMap<>();   
}

Globals has various things in it. usually HashMaps of things - I use it as a global json cache where each Context has an instance of this. Now overnight it appears the Application object can sometimes be empty. i.e. I use the app go away and go to sleep, go back to testing it in the morning and all the json caches are empty. But the user is still "logged in". I assume this is because of garbage collection on the OS.

Now. I could just refresh the json cache or force "logout" when the json cache is empty but there is a problem - it may be empty because there IS legitimately no json from the server. i.e "being empty" is no reason to go get more. What I need to be able to do is detect when android has flattened the cache, or at least know the minimum amount of time that Android will keep the Application extension.

Would it set everything to null?

Has anyone got any ideas? Bear in mind the context will re-initialise null HashMap members of the Application in the context in onCreate (which is required for reason outside the scope) because I declare the new but simply testing for "null" is not really an option. I suppose making a blank null that is changed only on json gather would be ok but I need to KNOW this will work or I lose yet another day chasing this (i.e it's VERY hard to test)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mr Heelis
  • 2,370
  • 4
  • 24
  • 34

1 Answers1

2

Now overnight it appears the Application object can sometimes be empty. i.e. I use the app go away and go to sleep, go back to testing it in the morning and all the json caches are empty.

Your process was terminated, most likely. See the documentation and the documentation.

What I need to be able to do is detect when android has flattened the cache

You are not informed when your process is terminated.

or at least know the minimum amount of time that Android will keep the Application extension

Your process can be terminated milliseconds after it leaves the foreground.

[Application works] fine as a data store

Only for data that you can easily reload from a persistent data store.

[Application] works works on multi thread

Only if you add your own thread-synchronization logic. There is nothing magic about properties and functions on Application that makes them thread-safe.

where there is no place to store mutable data - this is the best alternative

Any data that you wish to keep should be stored on disk (database, SharedPreferences, or other types of files) or on a server.

so my question remains how to mitigate it

Any data that you wish to keep should be stored on disk (database, SharedPreferences, or other types of files) or on a server. Use in-memory caches as caches.

because things like SQLite are useless they're not thread safe

If you use the same SQLiteDatabase instance for your operations, SQLite is thread-safe.

effectively making it impossible to run anything in parallel

You are certainly welcome to use other persistent data stores if you find SQLite to be distasteful.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491