2

I've an app that is used by carers to sign in to the system using their android phones. They scan a QRcode or swipe an NFC tag to get the client's information, scan time and location. The latter info constitutes a transaction. The transaction is placed in the phone's DB as well as submitted to a server via HTTP.

All works well as all transactions are put in DB and posted to web service until i have to make two transactions at the same time. A transaction is made by calling AsyncTask as it's a network call and so not to block UI thread. Both transactions are made to the web service but not all the data is sent to DB. I've commented out some code and made one Async post at a time and the data is then sent fine.

The problem

When one AsyncTask is running the other starts. When this happens the first AsyncTask doesn't work correctly. I have found the following thread where there is a similar problem and using a SerialExecutor seems to be the answer.

serialexecutor

https://stackoverflow.com/questions/6645203/android-asynctask-avoid-multiple-instances-running

Could anyone help me implement the 2 following AsyncTasks in a SerialExecutor, I'm fairly new to Android and i'm struggling to start this off. I'd like apd to execute and finish then apd3 to execute after. All in a serial fashion.

Thanks in advance Matt

    String temp_tagType = cursor.getString(cursor
                                                .getColumnIndex(LoginValidate.C_TYPE));
    String temp_tagCompId = cursor.getString(cursor
                                                .getColumnIndex(LoginValidate.C_COMPANY_ID));
   String temp_PersonId = cursor.getString(cursor
                                                .getColumnIndex(LoginValidate.C_PERSON_ID));
    String temp_tagName = cursor.getString(cursor
                                                .getColumnIndex(LoginValidate.C_NAME));
        String temp_tagId = cursor.getString(cursor
                                                .getColumnIndex(LoginValidate.C_TAG_ID));
        String temp_tagScanTime = cursor.getString(cursor
                                                .getColumnIndex(LoginValidate.C_TAG_SCAN_TIME));
    String temp_tagLatitude = cursor.getString(cursor
                                                .getColumnIndex(LoginValidate.C_TRANSACTIONS_LATITUDE));
    String temp_tagLongitude = cursor.getString(cursor
                                                .getColumnIndex(LoginValidate.C_TRANSACTIONS_LONGITUDE));
                                        if(temp_tagId == null){
                                            temp_tagId = "notag";
                                        }
                                        String manualM = "M";
                                         Log.e(TAG, " temp name and status = " + temp_tagName + " " + manualM);
                                    ////////insert the temp variables with the status set to M  
                                        ContentValues values = new ContentValues();
                                        values.putNull(LoginValidate.C_ID);
                                        values.put(LoginValidate.C_TYPE, temp_tagType);
                                        values.put(LoginValidate.C_COMPANY_ID, nfcscannerapplication.getCompId());
                                        values.put(LoginValidate.C_PERSON_ID, temp_PersonId);
                                        values.put(LoginValidate.C_NAME, temp_tagName);
                                        values.put(LoginValidate.C_TAG_ID, temp_tagId);
                                        values.put(LoginValidate.C_STATUS, manualM);
                                        values.put(LoginValidate.C_TAG_SCAN_TIME, manualLogoutTime);
                                        values.put(LoginValidate.C_TRANSACTIONS_LATITUDE, temp_tagLatitude);
                                        values.put(LoginValidate.C_TRANSACTIONS_LONGITUDE, temp_tagLongitude);

                                        DateTime now = new DateTime();
                                        DateTimeFormatter df = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
                                        String formattedNowTime = df.print(now);
                                        Log.e(TAG, "formattedNowTime = " + formattedNowTime);
                                        DateTimeFormatter df2 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
                                        String manualTime = df2.print(timeSetOnSpinner);
                                            Log.e(TAG, "about to put " + temp_tagName + " into DB");
                                        nfcscannerapplication.loginValidate.insertIntoTransactions(values);

                                        String[] params = new String[]{nfcscannerapplication.getCompId(), temp_tagId, temp_PersonId, nfcscannerapplication.getCarerID(),
                                                manualTime, formattedNowTime, manualM, getDeviceName(), temp_tagLatitude, temp_tagLongitude}; 
                                        AsyncPostData apd = new AsyncPostData();
                                        apd.execute(params);

                                         ///////////////now carry on as usual with the last actual tag that has been scanned
                                        Log.e(TAG, "about to insert the current record after inserting the manual logout");

                                        if(tagId == null){
                                            tagId = _tagId;
                                        } 



                                        ContentValues values3 = new ContentValues();
                                        values3.putNull(LoginValidate.C_ID);
                                        values3.put(LoginValidate.C_TYPE, tagType);
                                        values3.put(LoginValidate.C_COMPANY_ID, nfcscannerapplication.getCompId());
                                        values3.put(LoginValidate.C_PERSON_ID, tagPerson);
                                        values3.put(LoginValidate.C_NAME, tagUserName);
                                        values3.put(LoginValidate.C_TAG_ID, tagId);
                                        values3.put(LoginValidate.C_STATUS, IN);
                                        values3.put(LoginValidate.C_TAG_SCAN_TIME, tagScanTime.getMillis());
                                        values3.put(LoginValidate.C_TRANSACTIONS_LATITUDE, tagLatitude);
                                        values3.put(LoginValidate.C_TRANSACTIONS_LONGITUDE, tagLongitude);
                                        // make the current transaction out
                                        DateTime now3 = new DateTime();
                                        DateTimeFormatter df3 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
                                        String formattedNowTime3 = df3.print(now3);
                                        Log.e(TAG, "formattedNowTime = " + formattedNowTime3);

                                        String formattedTagScanTime3 = df3.print(tagScanTime);
                                        Log.e(TAG, "about to put " + tagUserName + " into DB"); 
                                        nfcscannerapplication.loginValidate.insertIntoTransactions(values3);

                                        String[] params3 = new String[]{nfcscannerapplication.getCompId(),  tagId, tagPerson, nfcscannerapplication.getCarerID(),
                                                formattedTagScanTime3, formattedNowTime, IN, getDeviceName(), tagLatitude, tagLongitude}; 
                                        AsyncPostData apd3 = new AsyncPostData();
                                        apd3.execute(params3);

.

Community
  • 1
  • 1
turtleboy
  • 8,210
  • 27
  • 100
  • 199
  • @njzk2 I've tried reading up on this http://developer.android.com/reference/java/util/concurrent/Executor.html but i don't know how to get started with it. I understand that if you call AsyncTask.execute().get() it should wait for the result, but it doesn't seem to. So i've tried a coiuple of things, i just need a bit of friendly advice – turtleboy Dec 03 '12 at 13:22
  • as of api11, you can call executeOnExecutor(AsyncTask.SERIAL_EXECUTOR) to run your asynctasks one at a time. Before that, that's a bit more complex – njzk2 Dec 03 '12 at 13:35
  • @njzk2 yep i had a feeling there'd be a problem. Some of the carer's phones will be gingerbread. I've found this link though http://stackoverflow.com/questions/9893813/can-an-android-asynctask-doinbackground-be-synchronized-to-serialize-the-task-ex – turtleboy Dec 03 '12 at 13:41
  • the link you posted seems a good solution. Reimplement the functionnality of running an asynctask on a fixedThreadPool is what I would do. (Also, I don't get why this sort of things is not in the support library.) – njzk2 Dec 03 '12 at 13:45

0 Answers0