开发者

android SharedPreferences synchronous problem adding array

hi I have a scenario like this

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")

    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.getStrin开发者_如何学Cg("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 < 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;
                }
            }
        }

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

prefs.getString("friendrequest1","queryaddnewfriend:name:UUID"); prefs.getString("friendrequest2","queryaddnewfriend:name:UUID"); prefs.getString("friendrequest3","queryaddnewfriend:name:UUID");

The friend requests are showed to the user one by one starting with friendrequest3.

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.

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"

Any thought about doing this better would be nice, thanks!


If it were me, I think I'd be using Sqlite for this, not preferences. It's much easier to manage and process rows of data, and you'll also find it's quicker. I've found that writing a series of preferences in quick succession is actually very, very slow, as writing to flash RAM can sometimes take a lot longer than you'd expect. For that reason when I write to prefs, I usually fire off a new thread to do it.

But my suggestion is.... use a database Sqlite to store the incoming messages, and put them behind a content provider.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜