android SharedPreferences synchronous problem adding array
<p>hi
I have a scenario like this</p>

<p>I made a chat program where a user can ad friends just like in yahoo messenger or hotmail messenger. If there are many friend requests coming in to one user i´m saving them dynamically
like this:     (Every request(string) look like this "queryaddnewfriend:name:UUID")</p>

<pre><code>    String msg = intent.getStringExtra("payload");
    String[] split = msg.split(":"); 
    String name = split[1];
    String UUID = split[2];
    if(msg.startsWith("queryaddnewfriend")){
        //queryaddnewfriend:name:UUID
        SharedPreferences prefs =  PreferenceManager.getDefaultSharedPreferences(getApplicationContext() );
        String frn = prefs.getString("friendrequest1", "");

        if(frn == ""){
            SharedPreferences.Editor editor = prefs.edit();
            String newReq = name.concat(":");
            newReq = newReq.concat(UUID); //create the name:UUID string             
            editor.putString("friendrequest".concat( Integer.toString(1)), newReq);
            editor.commit();
        }else{
            for(int index = 1; index &lt; 1000; ++index) {
                String line = prefs.getString("friendrequest".concat( Integer.toString(index)), "");
                if(line == ""){
                    Log.d(TAG,"create new *********************************************");
                    String newLine = name.concat(":");
                    newLine = newLine.concat(UUID); //create the name:UUID string
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putString("friendrequest".concat( Integer.toString(index)), newLine);
                    editor.commit();
                    break;
                }
            }
        }
</code></pre>

<p>So my SharedPreferences has non, one, or many rows like this
Dynamically added (notes the "friendrequest1" incrementation )</p>

<p>prefs.getString("friendrequest1","queryaddnewfriend:name:UUID");
 prefs.getString("friendrequest2","queryaddnewfriend:name:UUID");
 prefs.getString("friendrequest3","queryaddnewfriend:name:UUID");</p>

<p>The friend requests are showed to the user one by one starting with friendrequest3.<br>
The problem comes when the user accept a friend request.
I have to remove the friendrequest3 and at the same time there could be a
new friend request coming in and the code above is executed adding a new friendrequest4.</p>

<p>Im using C2DM so I have no control when Google cloud is executing the above code.
When i remove "friendrequest3" because user has responded ACCEPT or REJECT friend
I will do editor.remove("friendrequest3") editor.commit(); 
But if the above code has added "friendrequest4" my code will fail.
the complexity of this code is now quite high and i guess one can make it higher
and at the same time increasing the "bug factor"</p>

<p>Any thought about doing this better would be nice, thanks!</p>
