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










share|improve this question




















  • 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

















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










share|improve this question




















  • 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















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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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










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














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






share|improve this answer




























    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
    ...





    share|improve this answer





















    • 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











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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






    share|improve this answer

























      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






      share|improve this answer























        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






        share|improve this answer












        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 16:43









        Rene

        1,24115




        1,24115
























            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
            ...





            share|improve this answer





















            • 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















            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
            ...





            share|improve this answer





















            • 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













            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
            ...





            share|improve this answer












            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
            ...






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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


















            • 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


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini