Last active
March 9, 2019 17:02
-
-
Save parahall/3b25f11f8abecfe2a10d80e9ac196c80 to your computer and use it in GitHub Desktop.
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
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