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?