multithreading - Proper use of yieldIfContendedSafely() in an Android Multithreaded Application -


in application, using asynctask write data database in transaction. database accessed ui thread. while looking through available database methods, came across yieldifcontendedsafely(). seems method should used cases transaction being made separate thread. there hardly documentation on method other following:

temporarily end transaction let other threads run. transaction assumed successful far. not call settransactionsuccessful before calling this. when returns new transaction have been created not marked successful. assumes there no nested transactions (begintransaction has been called once) , throw exception if not case.

here how assume use method thread:

        try {         db.begintransaction();         //insert stuff database here          ...            // how use method?         boolean yielded = db.yieldifcontendedsafely();         if (yielded) {             try {                 thread.sleep(500);             } catch (interruptedexception e) {                 e.printstacktrace();             }         }            db.settransactionsuccessful();     } catch (sqlexception e) {         return false;     } {         db.endtransaction();         db.close();     } 

is correct way use method? alright use db.yieldifcontendedsafely() more once in same transaction, in between multiple writes different tables in database? suggestions?

pulling example code android libraries seems much simpler use that...

this taken com.android.providers.calendar.sqlitecontentprovider.java

@override public int bulkinsert(uri uri, contentvalues[] values) {     int numvalues = values.length;     mdb = mopenhelper.getwritabledatabase();     mdb.begintransactionwithlistener(this);     try {         (int = 0; < numvalues; i++) {             uri result = insertintransaction(uri, values[i]);             if (result != null) {                 mnotifychange = true;             }             mdb.yieldifcontendedsafely();         }         mdb.settransactionsuccessful();     } {         mdb.endtransaction();     }      onendtransaction();     return numvalues; } 

also looking source code function itself, seems that, if yielded, call defer execution of thread short period in case.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -