Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hyunji92
Copy link
Member

No description provided.

}
}b
return sInstance
}*/

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)
}

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
}

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 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants