我想使用架构项目(待办事项列表),但当我想运行项目时出现此错误
该项目正在使用不兼容版本(AGP 7.3.0-alpha07)的 Android Gradle 插件。最新支持的版本是 AGP 7.2.1
随着 Kotlin1.5的推出sealed interface,. 即使我知道类和接口之间的区别,我也不清楚使用sealed interfaceover的最佳实践和好处是什么sealed class
interface即使是简单的情况,我是否应该始终使用now ?还是将视具体情况而定?
谢谢
Obs:没有发现类似的问题,只是关于sealed classes
我是一个初学者学习coroutines。
不完全是,但我对 a 是什么有一点了解coroutine。
这suspend function也很难,但需要一点理解。
我正在一步步学习,但有些地方我不明白。
那是suspendCoroutine。在示例代码中,suspendCoroutine和Continuation在块中使用,但我不知道这两个是什么。
我看过其他网站,但找不到任何可以轻松解释的地方。
您能简单地解释一下suspendCoroutine和 的Continuation用途吗?如果可能的话,可以举个例子吗?
按照此处给出的说明进行操作 - https://developer.android.com/studio/preview/install-preview#change_your_update_channel
没有用于选择频道的下拉菜单。还有其他办法吗?
Android Studio 花栗鼠 | 2021.2.1 Patch 1 Build #AI-212.5712.43.2112.8609683,构建于 2022 年 5 月 18 日 运行时版本:11.0.12+0-b1504.28-7817840 x86_64 VM:OpenJDK 64 位服务器 VM by JetBrains sro macOS 12.4 GC :G1年轻代,G1老一代内存:2048M核心:4注册表:external.system.auto.import.disabled=true
我希望我的扩展功能有几个接收器。例如,我希望函数handle能够调用CoroutineScope和Iterable实例的方法:
fun handle() {
// I want to call CoroutineScope.launch() and Iterable.map() functions here
map {
launch { /* ... */ }
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这可能有效:
fun <T> (Iterable<T>, CoroutineScope).handle() {}
Run Code Online (Sandbox Code Playgroud)
但这给了我一个错误:
Function declaration must have a name
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用参数创建函数,但是
单个函数是否可以有多个接收器以及如何在没有参数的情况下做到这一点?
android kotlin extension-function kotlin-coroutines kotlin-context-receivers
我正在使用Room并实现了Dao,它返回了LiveData。添加下面的依赖关系,它工作正常。
implementation "androidx.room:room-runtime:2.1.0-alpha04"
kapt "androidx.room:room-compiler:2.1.0-alpha04"
Run Code Online (Sandbox Code Playgroud)
但是,当我添加新的Room协程依赖性时,如下所述。
implementation "androidx.room:room-runtime:2.1.0-alpha04"
implementation "androidx.room:room-coroutines:2.1.0-alpha04"
kapt "androidx.room:room-compiler:2.1.0-alpha04"
Run Code Online (Sandbox Code Playgroud)
下面是编译的代码
@Dao
interface AccountDao{
@Query("SELECT * FROM account_master")
suspend fun getAllAccounts(): List<Account>
}
Run Code Online (Sandbox Code Playgroud)
下面是给出错误的代码。
@Dao
interface AccountDao{
@Query("SELECT * FROM account_master")
suspend fun getAllAccounts(): LiveData<List<Account>>
}
Run Code Online (Sandbox Code Playgroud)
开始收到错误。
PlayGround/app/build/tmp/kapt3/stubs/debug/com/playground/www/x/datasource/dao/AccountDao.java:11: error: Not sure how to convert a Cursor to this method's return type (androidx.lifecycle.LiveData<java.util.List<com.playground.www.x.datasource.entity.Account>>).
public abstract java.lang.Object getAllAccounts(@org.jetbrains.annotations.NotNull()
Run Code Online (Sandbox Code Playgroud)
有人面临类似问题吗?
android android-room android-architecture-components kotlin-coroutines
我需要处理流收集中的当前值和先前值,因此我需要一些具有如下作用的运算符:
----A----------B-------C-----|--->
---(null+A)---(A+B)---(B+C)--|--->
Run Code Online (Sandbox Code Playgroud)
一个想法是这样的:
fun <T: Any> Flow<T>.withPrevious(): Flow<Pair<T?, T>> = flow {
var prev: T? = null
this@withPrevious.collect {
emit(prev to it)
prev = it
}
}
Run Code Online (Sandbox Code Playgroud)
但这样就无法控制执行第一个流程的上下文。有没有更灵活的解决方案?
coroutine kotlin kotlin-coroutines coroutinescope kotlin-flow
让我来问
我使用 kotlin 协程
@OptIn(DelicateCoroutinesApi::class)
GlobalScope.launch {
displaySura()
}
Run Code Online (Sandbox Code Playgroud)
并在构建选项卡中显示警告:
This annotation should be used with the compiler argument '-opt-in=kotlin.RequiresOptIn'
Run Code Online (Sandbox Code Playgroud)
如何解决这个警告?提前致谢
Kotlin 1.7when中将禁止对密封类/接口进行非详尽的声明。
我有一个sealed class State和它的孩子:
sealed class State {
object Initializing : State()
object Connecting : State()
object Disconnecting : State()
object FailedToConnect : State()
object Disconnected : State()
object Ready : State()
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,我只想处理特定的项目,而不是全部,例如:
val state: State = ... // initialize
when (state) {
State.Ready -> { ... }
State.Disconnected -> { ... }
}
Run Code Online (Sandbox Code Playgroud)
但我收到一条警告(在Kotlin 1.7中我猜这将是一个错误),说:
1.7 中将禁止在密封类/接口上使用非详尽的“when”语句,而是添加“Connecting”、“Disconnecting”、“FailedToConnect”、“Initializing”分支或“else”分支
else -> {}像下面的代码一样在这里使用空分支是一个好习惯吗?
when (state) {
State.Ready -> { ... }
State.Disconnected -> …Run Code Online (Sandbox Code Playgroud) 使用MVVM或MVP,我应该为模型,视图模型,视图等程序包命名,并在其中放置适当的类和接口,还是仅仅是在类结构中不可见的逻辑结构?
kotlin ×7
android ×6
coroutine ×3
build.gradle ×2
sealed-class ×2
android-architecture-components ×1
android-room ×1
annotations ×1
callback ×1
gradle ×1
interface ×1
kotlin-flow ×1
kotlin-when ×1
mvp ×1
mvvm ×1
structure ×1
suspend ×1