如何使用Anko doAsync处理UI线程上的Kotlin-Room异常

时间:2019-04-20 19:55:34

标签: android kotlin android-room android-jetpack anko

任何人都知道如何使用片段内的await挂起方法使用Anko doAsync处理UI线程上的Kotlin-Room异常。 因为在将await()doAsync一起使用时出现错误(无法解析的参考:等待)

注意:我使用的是kotlin_version ='1.3.21'和anko_version ='0.10.8'和android studio 3.3.2

FirstFragment.kt

class FirstFragment : Fragment() {


override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_first, container, false)
}

private var dbInstance: PersonDatabase? = null

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    doAsync {
      val instance = doAsync{ PersonDatabase.getInstance(this@FirstFragment.context!!) }
        dbInstance = instance.await()
  }

    button3.setOnClickListener {
        val name = editText.text.toString().trim()
        val age = editText2.text.toString().trim().toInt()
        val p =Person(null, name, age)
        doAsync {
            val insert = doAsync{ dbInstance?.personDau()?.insertData(p) }
            insert.await()
            insertMessage()
        }
    }
    button4.setOnClickListener {
        doAsync {
            val fetch = doAsync{ dbInstance?.personDau()?.getAll() }
            populateLis(fetch.await())
        }
    }

}

private fun populateLis(data: List<Person>?) {
    if (data == null){
        Toast.makeText(this.context,"No data",Toast.LENGTH_LONG).show()
        return
    }
    val adapter =object: ArrayAdapter<Person>(this.context, android.R.layout.simple_list_item_2, android.R.id.text1, data){
        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            val v = super.getView(position, convertView, parent)
            val p=data[position]
            v.findViewById<TextView>(android.R.id.text1).text=p.name
            v.findViewById<TextView>(android.R.id.text2).text=p.age.toString()
            return v
        }
    }
    personList.adapter = adapter
}

private fun insertMessage() {
    Toast.makeText(this.context,"Data inserted",Toast.LENGTH_LONG).show()

}

}

最高等级

buildscript {
    ext.kotlin_version = '1.3.21'
    repositories {
        google()
        jcenter()
        maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
        mavenCentral()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha01"


    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
    ext {
        androidSupportVersion = "28.0.0"

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Gradle应用级

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'




android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.martin.jetpack"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'android.arch.lifecycle:extensions:1.1.1'
    implementation 'com.android.support:design:28.0.0'
    implementation "com.android.support:customtabs:28.0.0"
    implementation 'com.android.support:mediarouter-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    def nav_version = "1.0.0-alpha01"

    implementation "android.arch.navigation:navigation-fragment:$nav_version" // For Kotlin use navigation-fragment-ktx
    implementation "android.arch.navigation:navigation-ui:$nav_version" // For Kotlin use navigation-ui-ktx

    // this for kotlin async
    def anko_version='0.10.8'

    implementation "org.jetbrains.anko:anko-commons:$anko_version"
    implementation "org.jetbrains.anko:anko-coroutines:$anko_version"

    // this is for android jetpack room
    def room_version = "1.1.1"
    implementation"android.arch.persistence.room:runtime:$room_version"
    kapt "android.arch.persistence.room:compiler:$room_version"
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    // Core dependency
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.0'
    // Android UI Programming
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.0"
    // RxJava2 & Reactive Programming
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.2.0'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-reactive:1.2.0'

}

1 个答案:

答案 0 :(得分:1)

在这种情况下,您可以使用协程延迟等待功能。我遇到了问题,并在此处how to get Deffered from DAO?

发布了答案