1

Solved: I had a problem with concurrent modification which I thought was due to calling my filter before the task was done, what ended up working for me was adding a call to an outside method in the onPostExecute that copied values to a copyonwritearraylist.

So I have an AsyncTask that parses json from a website and initializes a list when its done.

    inner class GetSchoolAsync : AsyncTask<String, String, String>() {
    lateinit var list: MutableList<Info>
    override fun onPreExecute() {
        // Before doInBackground
    }

    override fun doInBackground(vararg urls: String?): String {
        var urlConnection: HttpURLConnection? = null

        try {
            val url = URL(urls[0])

            urlConnection = url.openConnection() as HttpURLConnection
            urlConnection.connectTimeout = CONNECTON_TIMEOUT_MILLISECONDS
            urlConnection.readTimeout = CONNECTON_TIMEOUT_MILLISECONDS

            val inString = urlConnection.inputStream.bufferedReader().readText()

            publishProgress(inString)
        } catch (ex: Exception) {
            println("HttpURLConnection exception" + ex)
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect()
            }
        }

        return " "
    }

    override fun onProgressUpdate(vararg values: String?) {
        try {
            var list = mutableListOf<Info>()
            var data = Gson().fromJson(values[0], Read::class.java)

            for(item in data.schools){
                list.add(item)
               // println(item)
            }
            this.list = list



        } catch (ex: Exception) {
            println("JSON parsing exception" + ex.printStackTrace())
        }
    }

    override fun onPostExecute(result: String?) {
        // Done
        schoolList = this.list

    }
}

It is being executed in the mainactivity OnCreate along with another async function that renders a map. School list is a private lateinit var in the main activity.

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_maps)
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    val mapFragment = supportFragmentManager
            .findFragmentById(R.id.map) as SupportMapFragment
    GetSchoolAsync().execute("https://code.org/schools.json")
    mapFragment.getMapAsync(this)
}

I need to know when the json part is finished and the list is initialized so that I can add markers to the map. Any time I have tried to check the status of the Async task in the debugger its always showing that its either pending or running. When can I be certain that its finished?

mrfong
  • 11
  • 3
  • 3
    `onPostExecute()` is the method that is called when the AsyncTask is completed. – forpas Nov 10 '18 at 18:30
  • Thanks for the response, as a followup: how can I notify the main activity that its done so that I can perform operations with my initialized list? – mrfong Nov 10 '18 at 18:39
  • 1
    Read this https://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a and other related posts. – forpas Nov 10 '18 at 18:44
  • You can use a LocalBroadcastReceiver in the onPostExecute() method. – Rohit Singh Nov 12 '18 at 09:55

1 Answers1

0

You should better to use Interface for your case

 interface ReturnListener {
            fun result(result:String)
        }



  inner class GetSchoolAsync : AsyncTask<Object, String, String>() {
    lateinit var list: MutableList<Info>
    override fun onPreExecute() {
        // Before doInBackground
    }

    override fun doInBackground(vararg urls: Object?): String {
        var urlConnection: HttpURLConnection? = null
        var listener: ReturnListener? = null

        try {
            val url = URL(urls[0] as String)
            listener = urls[1] as ReturnListener

            urlConnection = url.openConnection() as HttpURLConnection
            urlConnection.connectTimeout = CONNECTON_TIMEOUT_MILLISECONDS
            urlConnection.readTimeout = CONNECTON_TIMEOUT_MILLISECONDS

            val inString = urlConnection.inputStream.bufferedReader().readText()

            publishProgress(inString)
        } catch (ex: Exception) {
            println("HttpURLConnection exception" + ex)
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect()
            }
        }

        return " "
    }

    override fun onProgressUpdate(vararg values: String?) {
        try {
            var list = mutableListOf<Info>()
            var data = Gson().fromJson(values[0], Read::class.java)

            for(item in data.schools){
                list.add(item)
               // println(item)
            }
            this.list = list



        } catch (ex: Exception) {
            println("JSON parsing exception" + ex.printStackTrace())
        }
    }

    override fun onPostExecute(result: String?) {
        // Done
        schoolList = this.list
     if (listener!=null)
       listener.result(result)

    }

and under OnCreate:

      override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_maps)
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        val mapFragment = supportFragmentManager
                .findFragmentById(R.id.map) as SupportMapFragment
        GetSchoolAsync().execute("https://code.org/schools.json", object: ReturnListener{
 override fun result(result:String) {

                 //ToDo: .......

             }
       })
        mapFragment.getMapAsync(this)
    }
slee
  • 239
  • 1
  • 4