New object from data class .copy()
up vote
1
down vote
favorite
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
android kotlin
add a comment |
up vote
1
down vote
favorite
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
android kotlin
1
you wouldn't have these problems if you just used immutable data. And no, it's not creating a deep copy so changing on object1 will also change on the copy
– s1m0nw1
Nov 7 at 15:36
@s1m0nw1 Could you elaborate?
– Adam Mitchell
Nov 7 at 15:37
Well, make your propertiesvar a: List<Long>
and don't expose the mutable version of List. This way you can make sure that once you got a list from the class it won't change afterward. instead of adding to your list you will reassign the list; instead of clearing you will reassign to an empty list etc
– s1m0nw1
Nov 7 at 15:42
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
android kotlin
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
android kotlin
android kotlin
edited Nov 7 at 15:18
asked Nov 7 at 15:03
Adam Mitchell
7051526
7051526
1
you wouldn't have these problems if you just used immutable data. And no, it's not creating a deep copy so changing on object1 will also change on the copy
– s1m0nw1
Nov 7 at 15:36
@s1m0nw1 Could you elaborate?
– Adam Mitchell
Nov 7 at 15:37
Well, make your propertiesvar a: List<Long>
and don't expose the mutable version of List. This way you can make sure that once you got a list from the class it won't change afterward. instead of adding to your list you will reassign the list; instead of clearing you will reassign to an empty list etc
– s1m0nw1
Nov 7 at 15:42
add a comment |
1
you wouldn't have these problems if you just used immutable data. And no, it's not creating a deep copy so changing on object1 will also change on the copy
– s1m0nw1
Nov 7 at 15:36
@s1m0nw1 Could you elaborate?
– Adam Mitchell
Nov 7 at 15:37
Well, make your propertiesvar a: List<Long>
and don't expose the mutable version of List. This way you can make sure that once you got a list from the class it won't change afterward. instead of adding to your list you will reassign the list; instead of clearing you will reassign to an empty list etc
– s1m0nw1
Nov 7 at 15:42
1
1
you wouldn't have these problems if you just used immutable data. And no, it's not creating a deep copy so changing on object1 will also change on the copy
– s1m0nw1
Nov 7 at 15:36
you wouldn't have these problems if you just used immutable data. And no, it's not creating a deep copy so changing on object1 will also change on the copy
– s1m0nw1
Nov 7 at 15:36
@s1m0nw1 Could you elaborate?
– Adam Mitchell
Nov 7 at 15:37
@s1m0nw1 Could you elaborate?
– Adam Mitchell
Nov 7 at 15:37
Well, make your properties
var a: List<Long>
and don't expose the mutable version of List. This way you can make sure that once you got a list from the class it won't change afterward. instead of adding to your list you will reassign the list; instead of clearing you will reassign to an empty list etc– s1m0nw1
Nov 7 at 15:42
Well, make your properties
var a: List<Long>
and don't expose the mutable version of List. This way you can make sure that once you got a list from the class it won't change afterward. instead of adding to your list you will reassign the list; instead of clearing you will reassign to an empty list etc– s1m0nw1
Nov 7 at 15:42
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Why not just create a new instance for every new run. You won't need the copy
function, just create a new instance after launching:
GlobalScope.launch {
uploadData(uploadBuffer)
}
uploadBuffer = new myDataClassVal()
BTW:
You should start your classes with an upper case letter, e.g. MyDataClassVal
. See Naming Conventions: https://kotlinlang.org/docs/reference/coding-conventions.html#naming-rulesan
You should use launch
instead of async
, because you don't need the result, see Kotlin Async vs Launch
add a comment |
up vote
1
down vote
If you want to clear the data only after the async task has finished, you can try using await.
uploadBuffer = myDataClassVal.copy()
GlobalScope.async {
uploadData(uploadBuffer)
}.await()
myDataClassVal.a.clear() //this will execute after async returns
...
The data needs to be cleared instantly, unfortunately!
– Adam Mitchell
Nov 7 at 15:37
Or... maybe it doesn't, let's experiment
– Adam Mitchell
Nov 7 at 15:38
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Why not just create a new instance for every new run. You won't need the copy
function, just create a new instance after launching:
GlobalScope.launch {
uploadData(uploadBuffer)
}
uploadBuffer = new myDataClassVal()
BTW:
You should start your classes with an upper case letter, e.g. MyDataClassVal
. See Naming Conventions: https://kotlinlang.org/docs/reference/coding-conventions.html#naming-rulesan
You should use launch
instead of async
, because you don't need the result, see Kotlin Async vs Launch
add a comment |
up vote
2
down vote
accepted
Why not just create a new instance for every new run. You won't need the copy
function, just create a new instance after launching:
GlobalScope.launch {
uploadData(uploadBuffer)
}
uploadBuffer = new myDataClassVal()
BTW:
You should start your classes with an upper case letter, e.g. MyDataClassVal
. See Naming Conventions: https://kotlinlang.org/docs/reference/coding-conventions.html#naming-rulesan
You should use launch
instead of async
, because you don't need the result, see Kotlin Async vs Launch
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Why not just create a new instance for every new run. You won't need the copy
function, just create a new instance after launching:
GlobalScope.launch {
uploadData(uploadBuffer)
}
uploadBuffer = new myDataClassVal()
BTW:
You should start your classes with an upper case letter, e.g. MyDataClassVal
. See Naming Conventions: https://kotlinlang.org/docs/reference/coding-conventions.html#naming-rulesan
You should use launch
instead of async
, because you don't need the result, see Kotlin Async vs Launch
Why not just create a new instance for every new run. You won't need the copy
function, just create a new instance after launching:
GlobalScope.launch {
uploadData(uploadBuffer)
}
uploadBuffer = new myDataClassVal()
BTW:
You should start your classes with an upper case letter, e.g. MyDataClassVal
. See Naming Conventions: https://kotlinlang.org/docs/reference/coding-conventions.html#naming-rulesan
You should use launch
instead of async
, because you don't need the result, see Kotlin Async vs Launch
answered Nov 7 at 16:43
Rene
1,24115
1,24115
add a comment |
add a comment |
up vote
1
down vote
If you want to clear the data only after the async task has finished, you can try using await.
uploadBuffer = myDataClassVal.copy()
GlobalScope.async {
uploadData(uploadBuffer)
}.await()
myDataClassVal.a.clear() //this will execute after async returns
...
The data needs to be cleared instantly, unfortunately!
– Adam Mitchell
Nov 7 at 15:37
Or... maybe it doesn't, let's experiment
– Adam Mitchell
Nov 7 at 15:38
add a comment |
up vote
1
down vote
If you want to clear the data only after the async task has finished, you can try using await.
uploadBuffer = myDataClassVal.copy()
GlobalScope.async {
uploadData(uploadBuffer)
}.await()
myDataClassVal.a.clear() //this will execute after async returns
...
The data needs to be cleared instantly, unfortunately!
– Adam Mitchell
Nov 7 at 15:37
Or... maybe it doesn't, let's experiment
– Adam Mitchell
Nov 7 at 15:38
add a comment |
up vote
1
down vote
up vote
1
down vote
If you want to clear the data only after the async task has finished, you can try using await.
uploadBuffer = myDataClassVal.copy()
GlobalScope.async {
uploadData(uploadBuffer)
}.await()
myDataClassVal.a.clear() //this will execute after async returns
...
If you want to clear the data only after the async task has finished, you can try using await.
uploadBuffer = myDataClassVal.copy()
GlobalScope.async {
uploadData(uploadBuffer)
}.await()
myDataClassVal.a.clear() //this will execute after async returns
...
answered Nov 7 at 15:36
deluxe1
1748
1748
The data needs to be cleared instantly, unfortunately!
– Adam Mitchell
Nov 7 at 15:37
Or... maybe it doesn't, let's experiment
– Adam Mitchell
Nov 7 at 15:38
add a comment |
The data needs to be cleared instantly, unfortunately!
– Adam Mitchell
Nov 7 at 15:37
Or... maybe it doesn't, let's experiment
– Adam Mitchell
Nov 7 at 15:38
The data needs to be cleared instantly, unfortunately!
– Adam Mitchell
Nov 7 at 15:37
The data needs to be cleared instantly, unfortunately!
– Adam Mitchell
Nov 7 at 15:37
Or... maybe it doesn't, let's experiment
– Adam Mitchell
Nov 7 at 15:38
Or... maybe it doesn't, let's experiment
– Adam Mitchell
Nov 7 at 15:38
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53192110%2fnew-object-from-data-class-copy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
you wouldn't have these problems if you just used immutable data. And no, it's not creating a deep copy so changing on object1 will also change on the copy
– s1m0nw1
Nov 7 at 15:36
@s1m0nw1 Could you elaborate?
– Adam Mitchell
Nov 7 at 15:37
Well, make your properties
var a: List<Long>
and don't expose the mutable version of List. This way you can make sure that once you got a list from the class it won't change afterward. instead of adding to your list you will reassign the list; instead of clearing you will reassign to an empty list etc– s1m0nw1
Nov 7 at 15:42