Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Race condition" when passing variable from onHandleIntent() to the application Singleton
    primarykey
    data
    text
    <p>I am just starting to learn android programming, and I came across multiple iterations when passing a complex variable "solved" in an IntentService (onHandleIntent method) back to the main Activity. I do not intend to have this data, available to other applications, other than mine (the running Application).</p> <p>First, I implemented Serializable to my complex data, and then passed it as an extra to the Intent sent back to the main Activity via BroadcastReceiver approach. This is done inside the <em>onHandleIntent</em> method implementation, which if I understood correctly, will run on another thread (not tying up the main thread).</p> <pre><code> Log.d(LOG_TAG, "Sending action update ready..."); Intent i = new Intent(ACTION_UPDATE_READY); // Intent extra begin i.putExtra("the_complex_data", complexSerializableData); // Intent extra end LocalBroadcastManager.getInstance(this).sendBroadcast(i); </code></pre> <p>In the main Activity, a receiver is setup like this:</p> <pre><code>private BroadcastReceiver onUpdate = new BroadcastReceiver() { public void onReceive(Context ctxt, Intent i) { Log.d(LOG_TAG, "Receiving ACTION_UPDATE_READY"); Bundle extras = i.getExtras(); if (extras != null) { ComplexData data = (ComplexData) extras.getSerializable("the_complex_data"); if (data != null &amp;&amp; data.size() &gt; 0) { updateAvailable(data); // refresh GUI } else { Toast.makeText(ctxt, "Connection is bad", Toast.LENGTH_LONG).show(); } } } }; </code></pre> <p>Later on, I found out, that Parceable will produce better performance vs. Serializable as you would find in most discussions here in SO.</p> <p>Then later on again, after Googling here and there, I found this <a href="http://developer.android.com/guide/faq/framework.html#3" rel="nofollow">topic</a> right off developer.android.com stating that you could use Singleton to pass around stuff!</p> <p>So I created a singleton class for my application, such that it holds a variable where I could "store" the results to this variable while inside the onHandleIntent method, and then later on, retrieve it from my main Activity, upon the receipt of the broadcast.</p> <p>Essentially, I didn't need to pass an Extra anymore, but changed it to set the variable in the application-wide Singleton (mAppGlobals):</p> <pre><code>mAppGlobals.setSharedData(complexSerializableData); </code></pre> <p>and then send off the broadcast as before. </p> <p>On the BroadcastReceiver at the main Activity, instead of pulling out the data from the received Intent extra, I just grabbed the data from the Singleton:</p> <pre><code>ComplexData data = mAppGlobals.getSharedData(); </code></pre> <p>It seems to work (still) on an emulator, after this change. However, I am unsure if there will be an occurrence, wherein the broadcast receiver code in my main activity, will grab an "older data" because the "onHandleIntent" running on another thread, hasn't finished writing to the Singleton instance variable (via setSharedData method). </p> <p>Does the android framework "ensure" that when the BroadcastReceiver code executes on my main Activity, the "onHandleIntent" method, running on a background thread (IntentService) actually finished writing to my global Singleton variable?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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