public class StarWarsService extends IntentService { | |
@Inject AppDatabase db; | |
public StarWarsService() { | |
super("StarWarsService"); | |
} | |
@Override public void onCreate() { | |
super.onCreate(); | |
((StarWarsApplication) getApplication()).getAppComponent().inject(this); | |
} | |
@Override protected void onHandleIntent(Intent intent) { | |
if (intent != null) { | |
String json = ResourcesUtil.loadJson(this); | |
Gson gson = new GsonBuilder().setDateFormat("MMM d, yyyy").create(); | |
ArrayList<StarWarsMovie> moviesList = | |
gson.fromJson(json, new TypeToken<ArrayList<StarWarsMovie>>() { | |
}.getType()); | |
db.beginTransaction(); | |
try { | |
db.starWarsMovieModel().deleteAll(); | |
for (StarWarsMovie starWarsMovie : moviesList) { | |
db.starWarsMovieModel().insertMovie(starWarsMovie); | |
} | |
db.setTransactionSuccessful(); | |
} | |
finally { | |
db.endTransaction(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment