CoroutineScope(SupervisorJob()) 是否在 Main 范围内运行?
Is CoroutineScope(SupervisorJob()) runs in Main scope?
我在做这个代码实验室
https://developer.android.com/codelabs/android-room-with-a-view-kotlin#13
有问题
class WordsApplication : Application() {
// No need to cancel this scope as it'll be torn down with the process
val applicationScope = CoroutineScope(SupervisorJob())
// Using by lazy so the database and the repository are only created when they're needed
// rather than when the application starts
val database by lazy { WordRoomDatabase.getDatabase(this, applicationScope) }
val repository by lazy { WordRepository(database.wordDao()) }
}
private class WordDatabaseCallback(
private val scope: CoroutineScope
) : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
INSTANCE?.let { database ->
scope.launch {
var wordDao = database.wordDao()
// Delete all content here.
wordDao.deleteAll()
// Add sample words.
var word = Word("Hello")
wordDao.insert(word)
word = Word("World!")
wordDao.insert(word)
// TODO: Add your own words!
word = Word("TODO!")
wordDao.insert(word)
}
}
}
}
这是我找到的代码,如您所见,它直接调用 scope.launch(...)
我的问题是:
不是所有 Room 操作都应该在非 UI 范围内 运行 吗?有人可以帮助我理解这一点吗?非常感谢!
Is CoroutineScope(SupervisorJob()) runs in Main scope?
没有。默认情况下 CoroutineScope()
使用 Dispatchers.Default
,可以在 documentation:
中找到
CoroutineScope() uses Dispatchers.Default for its coroutines.
isn't all the Room operations supposed to run in non-UI scope?
我对Room不是特别熟悉,但总的来说要看操作是挂起还是阻塞。您可以 运行 暂停任何 dispatcher/thread 的功能。示例中的 deleteAll()
和 insert()
函数被标记为 suspend
,因此您可以从 UI 和 non-UI 线程中 运行 它们。
我在做这个代码实验室 https://developer.android.com/codelabs/android-room-with-a-view-kotlin#13 有问题
class WordsApplication : Application() {
// No need to cancel this scope as it'll be torn down with the process
val applicationScope = CoroutineScope(SupervisorJob())
// Using by lazy so the database and the repository are only created when they're needed
// rather than when the application starts
val database by lazy { WordRoomDatabase.getDatabase(this, applicationScope) }
val repository by lazy { WordRepository(database.wordDao()) }
}
private class WordDatabaseCallback(
private val scope: CoroutineScope
) : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
INSTANCE?.let { database ->
scope.launch {
var wordDao = database.wordDao()
// Delete all content here.
wordDao.deleteAll()
// Add sample words.
var word = Word("Hello")
wordDao.insert(word)
word = Word("World!")
wordDao.insert(word)
// TODO: Add your own words!
word = Word("TODO!")
wordDao.insert(word)
}
}
}
}
这是我找到的代码,如您所见,它直接调用 scope.launch(...) 我的问题是: 不是所有 Room 操作都应该在非 UI 范围内 运行 吗?有人可以帮助我理解这一点吗?非常感谢!
Is CoroutineScope(SupervisorJob()) runs in Main scope?
没有。默认情况下 CoroutineScope()
使用 Dispatchers.Default
,可以在 documentation:
CoroutineScope() uses Dispatchers.Default for its coroutines.
isn't all the Room operations supposed to run in non-UI scope?
我对Room不是特别熟悉,但总的来说要看操作是挂起还是阻塞。您可以 运行 暂停任何 dispatcher/thread 的功能。示例中的 deleteAll()
和 insert()
函数被标记为 suspend
,因此您可以从 UI 和 non-UI 线程中 运行 它们。