Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. class SeedDatabaseWorker(
  2. context: Context,
  3. workerParams: WorkerParameters
  4. ) : CoroutineWorker(context, workerParams) {
  5.  
  6. override suspend fun doWork(): Result = coroutineScope {
  7. withContext(Dispatchers.IO) {
  8.  
  9. try {
  10. applicationContext.assets.open(DATA_FILENAME).use { inputStream ->
  11. JsonReader(inputStream.reader()).use { jsonReader ->
  12. val type = object : TypeToken<List<LegoSet>>() {}.type
  13. val list: List<LegoSet> = Gson().fromJson(jsonReader, type)
  14.  
  15. AppDatabase.getInstance(applicationContext).legoSetDao().insertAll(list)
  16.  
  17. Result.success()
  18. }
  19. }
  20. } catch (e: Exception) {
  21. Timber.e(e, "Error seeding database")
  22. Result.failure()
  23. }
  24. }
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement