Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are using the singleton <em>just</em> as shared memory between a background service which I assume is performing operations on a different thread, you may run into synchronization issues and or read inconsistent data.</p> <p>If the data in the singleton is not synchronized, you have to be careful because you are relying on your "protocol" to be sure that nobody is reading while your background thread is writing (which may lead to errors).</p> <p>On the other hand, if it is synchronized, you are risking to face anr error because the activity which reads the data may be blocked waiting the service to finish to write the data in the singleton. </p> <p>As the other said, you also have to keep in mind that your singleton may be freed if the os needs resources, and that your data may not be there anymore.</p> <p>I'd rather use an event bus such as <a href="http://square.github.com/otto/" rel="nofollow">otto</a> or <a href="https://github.com/greenrobot/EventBus" rel="nofollow">eventbus</a></p> <p>EDIT:</p> <p>Using a singleton as the entry point of background (intent) service is the approach suggested in 2010 Virgil Dobjanschi <a href="http://www.google.com/events/io/2010/sessions/developing-RESTful-android-apps.html" rel="nofollow">talk</a> about building rest client applications for android.</p> <p>The suggested approach is having a singleton that performs as controller of ongoing requests. Please consider also that request to intent service are already queued by the os, so you can throw several intents that will be processed sequentially by the intent service.</p> <p>Some time ago I also tried take that as a starting point for a library, which still remains unfinished. YOu can find the sources <a href="https://github.com/fedepaol/PostmanLib--Rings-Twice--Android" rel="nofollow">here</a></p> <p>What I would certainly not do is to store your data in the singleton. The approach I would prefer is to store the data in some persistent storage (such as sql / preferences / file / content provider) and let the client know of the change through a broadcast message (or, if you are using a content provider, through an observer).</p> <p>Finally, to some extent this is the approach followed by the <a href="https://github.com/octo-online/robospice" rel="nofollow">robospice</a> library, which looks quite mature and ships a lot of interesting features such as caching.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload