Last active
April 29, 2020 14:36
-
-
Save mitchtabian/358c80a8eae836f36bc406b1be2bcc83 to your computer and use it in GitHub Desktop.
Executing Tasks on a Background Thread
This file contains 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
private long[] saveNewWord(Word word){ | |
long[] returnValue = mDb.wordDataDao().insertWords(word); | |
if(returnValue.length > 0){ | |
Log.d(TAG, "saveNewWord: return value: " + returnValue.toString()); | |
} | |
return returnValue; | |
} | |
private List<Word> retrieveWords(String title){ | |
return mDb.wordDataDao().getWords(title); | |
} | |
private int updateWord(Word word){ | |
return mDb.wordDataDao().updateWord(word.getTitle(), word.getContent(), Utility.getCurrentTimeStamp(), word.getUid()); | |
} | |
private int deleteWord(Word word){ | |
return mDb.wordDataDao().delete(word); | |
} |
This file contains 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
class MyThreadHandler extends Handler { | |
public MyThreadHandler(Looper looper) { | |
super(looper); | |
} | |
@Override | |
public void handleMessage(Message msg) { | |
switch (msg.what) { | |
case Constants.WORD_INSERT_NEW: { | |
Log.d(TAG, "handleMessage: saving word on thread: " + Thread.currentThread().getName()); | |
Word word = msg.getData().getParcelable("word_new"); | |
Message message = null; | |
if (saveNewWord(word).length > 0) { | |
message = Message.obtain(null, Constants.WORD_INSERT_SUCCESS); | |
} else { | |
message = Message.obtain(null, Constants.WORD_INSERT_FAIL); | |
} | |
mMainThreadHandler.sendMessage(message); | |
break; | |
} | |
case Constants.WORD_UPDATE: { | |
Log.d(TAG, "handleMessage: updating word on thread: " + Thread.currentThread().getName()); | |
Word word = msg.getData().getParcelable("word_update"); | |
Message message = null; | |
int updateInt = updateWord(word); | |
if (updateInt > 0) { | |
message = Message.obtain(null, Constants.WORD_UPDATE_SUCCESS); | |
} else { | |
message = Message.obtain(null, Constants.WORD_UPDATE_FAIL); | |
} | |
mMainThreadHandler.sendMessage(message); | |
break; | |
} | |
case Constants.WORDS_RETRIEVE: { | |
Log.d(TAG, "handleMessage: retrieving words on thread: " + Thread.currentThread().getName()); | |
String title = msg.getData().getString("title"); | |
ArrayList<Word> words = new ArrayList<>(retrieveWords(title)); | |
Message message = null; | |
if (words.size() > 0) { | |
message = Message.obtain(null, Constants.WORDS_RETRIEVE_SUCCESS); | |
Bundle bundle = new Bundle(); | |
bundle.putParcelableArrayList("words_retrieve", words); | |
message.setData(bundle); | |
} else { | |
message = Message.obtain(null, Constants.WORDS_RETRIEVE_FAIL); | |
} | |
mMainThreadHandler.sendMessage(message); | |
break; | |
} | |
case Constants.WORD_DELETE: { | |
Log.d(TAG, "handleMessage: deleting word on thread: " + Thread.currentThread().getName()); | |
Word word = msg.getData().getParcelable("word_delete"); | |
Message message = null; | |
if (deleteWord(word) > 0) { | |
message = Message.obtain(null, Constants.WORD_DELETE_SUCCESS); | |
} else { | |
message = Message.obtain(null, Constants.WORD_DELETE_FAIL); | |
} | |
mMainThreadHandler.sendMessage(message); | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment