-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Long running service in background #21
Comments
This requires some changes to the current design of the app. I believe my original reasoning for having a service act as the software agent component with Ogame's servers was so that data loading can occur in the background if the user taps the home button on the device. When the user returns to the app, the data will be already be loaded and processed, and the client of the service can immediately begin displaying the data to the user without having to wait for a network load. I did not want the service to be long running because if the Android system needs memory, then I do not want to make the overall Android experience worse by increasing the priority of the app. Although this seems like a problem more suited for Loaders, I also want the service to run while the app is in the foreground for users that do a lot of switching between various different universes (playing multiple accounts). If they do a network request for one account, they are able to switch to a separate account in the mean time so the network request for the first account continues. Perhaps it makes more sense to redesign the app in the following fashion:
In any case for item 3, these 3 points solve my requirements for the app. Point 1 will allow OgameAgent objects to remain in memory when the app goes to the background. Point 2 allows the service to run in the background if the app gets dismissed, and it will update the OgameAgent object as needed. |
The 3. can be easily achieved with the following pattern :
A workaround can be done easily with the following signature :
Warning, using IntentService is not efficient in order to store the Manager information. Those services are only meant to perform actions and can be "cleaned" without any notice (for instance, when the Intent queue is empty, there are meant to be closed soon after the detection). A typical Service can stay in memory quite longer (until the system consider that due to RAM usage, sleep inactivity etc... it must be killed) The final persistent usage is the startForeground() (with a notification) |
An other way to keep the Agents event easy to maintaing :
|
I have a local branch I have been working on for the past week. I went with using EventBus to inform activities and fragments of updated OgameAgent objects. I'm using an IntentService to run the action of making network requests and updates, but I'm taking the responsibility of managing the OgameAgent objects out into a separate class (essentially, references to the OgameAgent objects is maintained through a static variable referencing a data structure - in my case, a LongSparseArray). This design means that if the system needs to reclaim system resources by killing the process, then the OgameAgent manager and the OgameAgent objects it managers will be cleared as intended. If the process is not killed when a user returns, then the OgameAgent manager and its objects are already sitting in memory ready to go. This is also intended. I like the idea of having multiple EventBuses - 1 for each OgameAgent. I will update my work with that suggestion before opening a PR. Thanks! |
Instead of using a static reference, you should use the Application instance of the app to store it. The Application is the first and latest object of the "app". Hence, logically, it is good to store the reference in it (not a static one but a direct field) |
Please explain what you mean by saying the Application object is the latest object of the app. |
…d design mentioned in Github issue #21
…g with the new message-passing design outlined in #21. This redesign is to remove the long-running service from the background.
I meant in the allocation lifecycle. The Application instance is loaded and then, the different entry points defined in the manifest: activity, receivers etc... Note that when the app is compltely killed, the onTerminte() function can not be called (but everything is released) so keeping a non-static OgameAgents reference in the ApplicationController defined in the code make sense |
I still don't see the advantage, nor a disadvantage of having the reference sit within an Application object vs. having the reference be static within the manager's own class. It seems to me to be purely a code aesthetic preference, but I'll define a reference to the manager in the Application class before opening the PR. |
This application does not need a long-running service in the background. This issue is open until the long-running service is no longer long-running so that the app plays nicer with other apps on each device.
The text was updated successfully, but these errors were encountered: