20645

Initialize database using kotlin in AsyncTask

Question:

In my doInBackground I declare and Initialize db, I got an error which say type mismatch. What should I put instead of putting this?

var dbHelper: MyDBHelper? = null dbHelper = MyDBHelper(this)

What should I put, There say Required: Context!

And this is my async task code, That Problem at dbHelper = MyDBHelper(this).

private class UpgradeDB(textView: TextView?) : AsyncTask<String, String, String>() { var innerTextView: TextView? = textView override fun onPreExecute() { innerTextView!!.visibility = View.VISIBLE } override fun doInBackground(vararg params: String): String? { val filename = "eBOSSInv_Upgrade.sql" val sdcard = Environment.getExternalStorageDirectory() val file = File(sdcard, filename) if (!file.exists()) isCancelled try { var dbHelper: MyDBHelper? = null dbHelper = MyDBHelper(this) dbHelper!!.writableDatabase.use { db -> var intTotalLine = 0 var intLine = 1 BufferedReader(FileReader(file)).useLines { _ -> intTotalLine++ } BufferedReader(FileReader(file)).use { r -> r.lineSequence().forEach { if (it.isNotEmpty()) { db!!.execSQL(it) publishProgress(String.format("Updating %s/%s records", intLine, intTotalLine)) intLine++ } } } } } catch (e: Exception) { } return null } override fun onProgressUpdate(vararg text: String) { innerTextView!!.text = text[0] } override fun onPostExecute(result: String?) { innerTextView!!.text = "" } override fun onCancelled() { } }

Answer1:

You need to pass context into Async Task.

Example:

llUpdate.setOnClickListener { UpgradeDB(txtUpdate!!, getApplication()).execute("", "", "") }

Then in UpgradeDB, initialize the context.

private class UpgradeDB(textView: TextView?, context: Context) : AsyncTask<String, String, String>() { var innerTextView: TextView? = textView var mContext:Context? = context // initialize context }

And finally

dbHelper = MyDBHelper(mContext)

Answer2:

It depends where you try to create this object. Unless you do it in a class that extends Context (like Activity) it is not supposed to work. You can try such construct:

There are two ways you gan go:

The simpler way: Declare your AsyncTask as inner class inside tour activity and reference context using label. To declare class as inner you need to place it inside another class and add <em>inner</em> keyowrd:

class A { inner class B }

Now you can reference context from inside your async task like that:

dbHelper = DbHelper([ActivityName]@this)

Nevertheless, this is easy way to cause a memory leak, you can read more here: <a href="https://stackoverflow.com/questions/46273753/android-asynctask-memory-leaks" rel="nofollow">Android AsyncTask memory leaks</a>

The better way is to extract AsyncTask to another class and pass an application context to it, instead of Activity.

Recommend

  • Is there a way to view MonetDB Function signatures
  • How to screen record iOS-real device with command?
  • Accessing root files (/system, /dev) from a native library in Android application
  • Android emulator sd card image already in use
  • Android java.lang.IllegalStateException in onRequestPermissionsResult()
  • Interpolation method that does not add unnecessary extremums
  • Mocking Non-Standard Events in F# Foq
  • data.table replicate rows after join?
  • Replace Fragment with another on back button
  • Wrap C++ function using Boost Reflect or another C++ reflection library
  • android Navigation Bar hiding and persantage of usable screen overlap
  • Swift Initialization Rule Confusion
  • Change Checkbox value without raising event
  • LiveData is abstract android
  • CSS - Cannot get one spanned style to override another inherited style and align left
  • Authentication failed with Azure Active Directory in Windows Phone
  • How to get listview position?
  • Regarding starting the threads on a condition
  • Group list of tuples by item
  • Center align outputs in ipython notebook
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • Zoom in and out of jPanel
  • GridView breaks while scrolling
  • Rails Find when some params will be blank
  • jQuery .attr() and value
  • How to install a .deb file on a jailbroken iphone programmatically?
  • Handling un-mapped Rest path
  • NHibernate Validation Localization with S#arp Architecture
  • how to do an event when i swipe from fragment to the other
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • PHP - How to update data to MySQL when click a radio button
  • Counter field in MS Access, how to generate?
  • apache spark aggregate function using min value
  • unknown Exception android
  • Can't mass-assign protected attributes when import data from csv file
  • reshape alternating columns in less time and using less memory
  • failed to connect to specific WiFi in android programmatically