2012-05-05 4 views
0

데이터베이스 작업을 분산 스레드로 만들고 싶습니다. 우선 ThreadLooper을 작성하여 Runnables에 게시하고 DB 작업을 시작합니다.필요합니까?

그것은 다음과 같습니다

이제
import android.os.Handler; 
import android.os.Handler.Callback; 
import android.os.HandlerThread; 
import android.os.Message; 

/** 
* @author 
* @version 1.0 This class is used as ThreadLooper to make the database 
*   operation CRUD , this looper is singlton across the app 
* 
*/ 
public class DBThreadLooper extends HandlerThread { 
    public Handler mHandler; 

    private DBThreadLooper(String name) { 
     super(name); 

    } 

    private static DBThreadLooper mInstance; 

    public static DBThreadLooper newInstance() { 

     if (mInstance == null) { 
      mInstance = new DBThreadLooper("DATA BASE THREAD LOOPER "); 
      mInstance.start(); 
     } 
     return mInstance; 
    } 

    @Override 
    public synchronized void start() { 
     super.start(); 
     waitUntilReady(); 
    } 

    private void waitUntilReady() { 
     mHandler = new Handler(getLooper(), new Callback() { 

      public boolean handleMessage(Message msg) { 

       return true; 
      } 
     }); 
    } 

    @Override 
    protected Object clone() throws CloneNotSupportedException { 
     throw new CloneNotSupportedException(); 
    } 

} 

I

private void handleFavButton() { 
     int index = viewPager.getCurrentItem(); 
     Cursor c = mAdapter.getAdapterCursor(); 
     c.moveToPosition(index); 
     final String quote_id = c.getString(c.getColumnIndex(QuoteTableMetaData._ID)); 

     final int is_fav = c.getInt(c.getColumnIndex(QuoteTableMetaData.C_IS_FAVORITE)); 


     if(is_fav == 0){ 
      DBThreadLooper looper = DBThreadLooper.newInstance(); 
      looper.mHandler.post(new Runnable() { 

       public void run() { 
        //1. make it 1 
        QuoteTableMetaData qTable = QuoteTableMetaData 
          .getInstance(); 
        ContentValues values = new ContentValues(); 
        values.put(QuoteTableMetaData.C_IS_FAVORITE, new Integer(1)); 
        qTable.update(DBUtils.getDBHelper(getApplicationContext()) 
          .getWritableDatabase(), values, 
          QuoteTableMetaData._ID + "= ?", 
          new String[] { quote_id }); 
        //2. insert a new record in Fav Table with the id 
        FavouriteQuoteTable fTable = FavouriteQuoteTable 
          .getInstance(); 
        values.clear(); 
        values.put(FavouriteQuoteTable.C_QUOTE_ID, quote_id); 
        fTable.insert(DBUtils.getDBHelper(getApplicationContext()) 
          .getWritableDatabase(), null, values); 
       } 
      }); 
     } 
     else{ 
      DBThreadLooper looper = DBThreadLooper.newInstance(); 
      looper.mHandler.post(new Runnable() { 

       public void run() { 
        //1.make it 0 
        QuoteTableMetaData qTable = QuoteTableMetaData 
          .getInstance(); 
        ContentValues values = new ContentValues(); 
        values.put(QuoteTableMetaData.C_IS_FAVORITE, new Integer(0)); 
        qTable.update(DBUtils.getDBHelper(getApplicationContext()) 
          .getWritableDatabase(), values, 
          QuoteTableMetaData._ID + "=?", 
          new String[] { quote_id }); 
        // 2. delete record with id from fav Table 
        FavouriteQuoteTable fTable = FavouriteQuoteTable 
          .getInstance(); 
        fTable.delete(DBUtils.getDBHelper(getApplicationContext()) 
          .getWritableDatabase(), 
          FavouriteQuoteTable.C_QUOTE_ID + "=?", 
          new String[] { quote_id }); 
       } 
      }); 

     } 

내가 있도록 방법 volatilequote_idis_fav을해야 수행합니다 DB 작업을 할 것이다이 방법이 내 메서드가 동기화 문제로 실행되지 않습니다.

답변

1

mutlithread 문제가 없습니다. 로컬 변수입니다 (최종적으로). 즉, handleFavButton 메서드를 호출 할 때마다 해당 인스턴스가 별도로 있으며 변수에 액세스하는 다른 호출이 간섭하지 않습니다.

+0

고맙습니다. – user4o01