• 10
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

anyone knows how to handling Kotlin-Room exceptions on UI thread with Anko doAsync using await suspended method inside a fragment. because I get an error (Unresolved reference: await) while using await() with doAsync

Note: I'm using kotlin_version = '1.3.21' with anko_version='0.10.8' and 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()

}

}

Gradle Top-level

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 App-level

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'

}

in such cases you can use Coroutines Deferred Await function. I faced the problem and posted answer here how to get Deffered from DAO?

  • 1
Reply Report
      • 1
    • actually, my Gradle App-level dependency was updated to be implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.0" but it's still await() not working with Anko doAsync