Last active
September 11, 2015 07:46
-
-
Save mokemokechicken/1864f8c75a7c9e705c17 to your computer and use it in GitHub Desktop.
Example for Process Something in one thread. (for example about SQLite)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.sqlite; | |
| import android.database.Cursor; | |
| import android.database.sqlite.SQLiteDatabase; | |
| import android.os.Handler; | |
| import android.os.HandlerThread; | |
| import android.os.Looper; | |
| import com.example.sqlite.RbSQLiteHelper; | |
| /** | |
| * Created by k_morishita on 15/09/11. | |
| */ | |
| public class SQLiteManager { | |
| private final Handler mHandler; | |
| private final Handler mMainThreadHandler; | |
| public SQLiteManager() { | |
| HandlerThread handlerThread = new HandlerThread("SQLiteManager"); | |
| handlerThread.start(); | |
| mHandler = new Handler(handlerThread.getLooper()); | |
| mMainThreadHandler = new Handler(Looper.getMainLooper()); | |
| } | |
| public <T> void query(final SqlQuery<T> executor) { | |
| query(executor, mMainThreadHandler); | |
| } | |
| public <T> void query(final SqlQuery<T> executor, final Handler callbackHandler) { | |
| mHandler.post(new Runnable() { | |
| @Override | |
| public void run() { | |
| // TODO: Readable を取るほうが自然だろう・・・ | |
| SQLiteDatabase database = RbSQLiteHelper.getWritable(null); // Readable!! | |
| try { | |
| final T ret = executor.exec(database); | |
| callbackHandler.post(new Runnable() { | |
| @Override | |
| public void run() { | |
| executor.onSuccess(ret); | |
| } | |
| }); | |
| } catch (final Exception e) { | |
| callbackHandler.post(new Runnable() { | |
| @Override | |
| public void run() { | |
| executor.onError(e); | |
| } | |
| }); | |
| } | |
| } | |
| }); | |
| } | |
| public void exec(final SqlExec executor) { | |
| exec(executor, mMainThreadHandler); | |
| } | |
| public void exec(final SqlExec executor, final Handler callbackHandler) { | |
| mHandler.post(new Runnable() { | |
| @Override | |
| public void run() { | |
| try { | |
| SQLiteDatabase database = RbSQLiteHelper.getWritable(null); | |
| try { | |
| database.beginTransaction(); | |
| executor.exec(database); | |
| database.setTransactionSuccessful(); | |
| } finally { | |
| database.endTransaction(); | |
| } | |
| callbackHandler.post(new Runnable() { | |
| @Override | |
| public void run() { | |
| executor.onSuccess(); | |
| } | |
| }); | |
| } catch (final Exception e) { | |
| callbackHandler.post(new Runnable() { | |
| @Override | |
| public void run() { | |
| executor.onError(e); | |
| } | |
| }); | |
| } | |
| } | |
| }); | |
| } | |
| public interface SqlQuery<T> { | |
| T exec(SQLiteDatabase database); | |
| void onSuccess(T result); | |
| void onError(Exception e); | |
| } | |
| public interface SqlExec { | |
| void exec(SQLiteDatabase database); | |
| void onSuccess(); | |
| void onError(Exception e); | |
| } | |
| } | |
| class AnyEntity {} | |
| class Usage { | |
| private SQLiteManager mSqlManager; | |
| public void whenRead() { | |
| mSqlManager.query(new SQLiteManager.SqlQuery<AnyEntity>() { | |
| @Override | |
| public AnyEntity exec(SQLiteDatabase database) { | |
| Cursor cursor = database.rawQuery("...", null); | |
| AnyEntity ret = null; // fetch data from cursor | |
| return ret; | |
| } | |
| @Override | |
| public void onSuccess(AnyEntity result) { | |
| // do something | |
| } | |
| @Override | |
| public void onError(Exception e) { | |
| // handle exception | |
| } | |
| }); | |
| } | |
| public void whenWrite() { | |
| mSqlManager.exec(new SQLiteManager.SqlExec() { | |
| @Override | |
| public void exec(SQLiteDatabase database) { | |
| // do something with database | |
| } | |
| @Override | |
| public void onSuccess() { | |
| // do something | |
| } | |
| @Override | |
| public void onError(Exception e) { | |
| // handle exception | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment