blob: 4e435da9507976a28b71d1d18f5268f9991f2408 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package SubredditDatabase;
import android.app.Application;
import android.arch.lifecycle.LiveData;
import android.os.AsyncTask;
public class SubredditRepository {
private SubredditDao mSubredditDao;
private LiveData<SubredditData> mSubredditLiveData;
SubredditRepository(Application application, String subredditName) {
SubredditRoomDatabase db = SubredditRoomDatabase.getDatabase(application);
mSubredditDao = db.subredditDao();
mSubredditLiveData = mSubredditDao.getSubredditLiveDataByName(subredditName);
}
LiveData<SubredditData> getSubredditLiveData() {
return mSubredditLiveData;
}
public void insert(SubredditData subredditData) {
new InsertAsyncTask(mSubredditDao).execute(subredditData);
}
private static class InsertAsyncTask extends AsyncTask<SubredditData, Void, Void> {
private SubredditDao mAsyncTaskDao;
InsertAsyncTask(SubredditDao dao) {
mAsyncTaskDao = dao;
}
@Override
protected Void doInBackground(final SubredditData... params) {
mAsyncTaskDao.insert(params[0]);
return null;
}
}
}
|