问题
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)
回答1:
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.
回答2:
This is not entirely an answer to your question but it's too long to comment, but you shouldn't be storing mutable data in your class which extends from Application:
see this for more info (https://github.com/codepath/android_guides/wiki/Understanding-the-Android-Application-Class)
which specifically says that:
...you should never store mutable instance data inside the Application object because if you assume that your data will stay there, your application will inevitably crash at some point with a NullPointerException. The application object is not guaranteed to stay in memory forever, it will get killed. Contrary to popular belief, the app won’t be restarted from scratch. Android will create a new Application object and start the activity where the user was before to give the illusion that the application was never killed in the first place.
So, the application class also gets killed off as most other things, meaning that (in your case) it probably gets removed at one point, which is causing most of your problems
possibly related: Value of Application Context Variables Lost on Application Error
see also : http://www.developerphil.com/dont-store-data-in-the-application-object/
Is it safe to use static class variables in an android application
来源:https://stackoverflow.com/questions/58024949/global-object-based-on-extend-application-evaporates-overnight-why