Update memory model from Service
I plan to write an IntentService to download new packages (new items for the app). My only problem is, that when I broadcast the end of the downloads, I have to update the memory model, if the app is running. If it is not running, nothing has to be done, as the app will create the memory model from the DB when it starts. Performance-wise, the UI works from the memory, not from the db. Any idea how should I approach, or I'm totally on the wrong track?
EDIT: For clarity: Let's say I have MyIntentService, that inserts new lines to my DB, and then fires a BroadcastIntent, which is received by MyBroadcastReceiver. Now the problem I face is, that the instance of MyBroadcastReceiver should notify somehow the Model class, which is a singleton. The problem is, that this Model (singleton) instance should be available only if my app is running, as this is consumed by only the UI (using only DB would be way to slow). 开发者_StackOverflow中文版No need for notifying my Model instance if there is no UI to use it. I can't make my Model to be a BroadcastReceiver, as they are initiated any time, and they can't be singleton either (as far as I know).
I solved this by having the UI register as the broadcast receiver and update the Model as required. In my solution if the UI exists the Model will always be instantiated.
Another solution is to check if the Model is instantiated and only proceeding if it is.
It's a bit difficult to answer and say something having only your words without code. May make additional seperated part of your project that will initialize/create your "memory model" from DB and this part should be undepedent one, so you can use it from anywhere anytime, during starting your activities/application or when get broadcast.
精彩评论