I have a data class in Kotlin:
data class myDataClass(
    var a: ArrayList<Long> = ArrayList(),
    var b: ArrayList<Long> = ArrayList(),
    var c: ArrayList<Long> = ArrayList(),
    ...
)
private val myDataClassVal: myDataClass = myDataClass()
I use this data class to store data acquired over BLE that will, when each ArrayList is at a certain length, POST to a REST API. After this POST the data within myDataClass is .clear()ed and the process will repeat.
The BLE portion of the application is time sensitive and each POST is taking approximately 1 second; my solution to this is to run my POST function asynchronously; as opposed to running on the same thread as the BLE code. I do this in the following way:
GlobalScope.async { 
    uploadData(myDataClassVal) 
}
myDataClassVal.a.clear()
myDataClassVal.b.clear()
myDataClassVal.c.clear()
Unfortunately, where I am clearing the data in myDataClass immediately after the async function call the data is actually cleared from the data class before it is serialised and POSTed. 
In an attempt to solve this problem I created a duplicate of myDataClass right before uploading and pass this into the async upload function. The duplicate is created using the .copy() function as described here:
uploadBuffer = myDataClassVal.copy()
GlobalScope.async {
    uploadData(uploadBuffer)
}
myDataClassVal.a.clear()
....
However, uploadBuffer is still completely empty. If I create a copy of myDataClass in the same way and POST on the same thread:
uploadBuffer = myDataClassVal.copy()
uploadData(uploadBuffer)
myDataClassVal.a.clear()
....
Then it works just fine.
So, I think my issue is that uploadBuffer is just a pointer to myDataClass. If this is the case, how do I create a new object that is a duplicate of myDataClass to use in my async POST?
Thanks, Adam
 
     
    