SHARE
TWEET

Untitled

a guest Sep 17th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top