Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SeedDatabaseWorker(
- context: Context,
- workerParams: WorkerParameters
- ) : CoroutineWorker(context, workerParams) {
- override suspend fun doWork(): Result = coroutineScope {
- withContext(Dispatchers.IO) {
- try {
- applicationContext.assets.open(DATA_FILENAME).use { inputStream ->
- JsonReader(inputStream.reader()).use { jsonReader ->
- val type = object : TypeToken<List<LegoSet>>() {}.type
- val list: List<LegoSet> = Gson().fromJson(jsonReader, type)
- AppDatabase.getInstance(applicationContext).legoSetDao().insertAll(list)
- Result.success()
- }
- }
- } catch (e: Exception) {
- Timber.e(e, "Error seeding database")
- Result.failure()
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement