-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hyundeeeee:DatabaseCreator.kt 변환 #2
base: master
Are you sure you want to change the base?
Conversation
} | ||
}b | ||
return sInstance | ||
}*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
val sInstance by lazy { DatabaseCreator()}
synchronized fun getInstance() = sInstance
// 다 없엔다
} | ||
}.execute(context.applicationContext) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fun createDb(context: Context) {
Log.d("DatabaseCreator", "Creating DB from " + Thread.currentThread().name)
if (!mInitializing.compareAndSet(true, false)) return // Already initializing
mIsDatabaseCreated.value = false // Trigger an update to show a loading screen.
object : AsyncTask<Context, Void, Void>() {
override fun doInBackground(vararg params: Context): Void? {
Log.d("DatabaseCreator",
"Starting bg job " + Thread.currentThread().name)
val context = params[0].applicationContext.apply {
deleteDatabase(DATABASE_NAME) // Reset the database to have new data on every run.
}
// Build the database!
val db = Room.databaseBuilder<AppDatabase>(context.applicationContext,
AppDatabase::class.java, DATABASE_NAME).build()
// Add a delay to simulate a long-running operation
addDelay()
// Add some data to the database
DatabaseInitUtil.initializeDb(db)
Log.d("DatabaseCreator",
"DB was populated in thread " + Thread.currentThread().name)
database = db
return null
}
override fun onPostExecute(ignored: Void) {
// Now on the main thread, notify observers that the db is created and ready.
mIsDatabaseCreated.value = true
}
}.execute(context.applicationContext)
}
database = db | ||
return null | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mIsDatabaseCreated.value = false// Trigger an update to show a loading screen.
Observable.just(context.applicationContext)
.subscribeOn(Schedulers.io())
.doOnNext{it.deleteDatabase(DATABASE_NAME)}
.map { Room.databaseBuilder(it,
AppDatabase::class.java, DATABASE_NAME).build() }
.doOnNext{
addDelay()
DatabaseInitUtil.initializeDb(it)
database = it
}
.observeOn(AndroidSchedulers.mainThread())
.subscribe { mIsDatabaseCreated.value = true }
No description provided.