iCloud deleting half of my table

Hello,

I am trying to upload a table to iCloud. When I download it again, it is broken, I have absolutely no idea why that happens. It always cuts the table where the first “false” is. I have tried that with several options, booleans and strings, it is always the same.

theTable = {true, true, true, false, false} iCloud.set("theTable", json.encode(theTable)) --- theTableDownload = iCloud.get("theTable") theTableDownload = json.decode(theTableDownload) -- this is the remaining table: {true, true true}

Have you tried with 1 and 0 instead of true and false?

Yes, and also with strings. But my original table has more than 5 values, it has 144 x true, then 4 x false and then 1 x true again. Could that be a problem?

Edit:

I have to make a new project, it could be that I am just overwriting the table so often that I am confused with what it should look like. I will write as soon as I find out something new

@philipp3

Your code works. If I run this code it’s correct:

local iCloud = require( "plugin.iCloud" ) local json = require("json") theTable = {true, true, true, false, false} iCloud.set("theTable", json.encode(theTable)) print ("before", json.prettify(theTable)) --- theTableDownload = iCloud.get("theTable") if (theTableDownload ~= nil) then theTableDownload = json.decode(theTableDownload) print ("after", json.prettify(theTableDownload)) end

Output:

before [true,true,true,false,false] after [true,true,true,false,false]

Read the gotchas for iCould

@adrianm

But in this case he’s storing the array as a JSON string, which is perfectly fine.

@inglemar, I was referring to this in particular

Keys should be less than 64 symbols (bytes).

I would imagine a JSON string of 149 booleans would be longer that 64 bytes?

True, that should be taken into consideration, but philipp3 claims that even with 5 values it was failing, which in my testing didn’t fail.

No worries, I think you missed a follow up post saying "But my original table has more than 5 values, it has 144 x true, then 4 x false and then 1 x true again. Could that be a problem?"

But that limitation is for keys, not values. :slight_smile:

The key is his example is “theTable”.

I modified the example to generate 150 values, and the returned result from iCloud is correct.

local iCloud = require( "plugin.iCloud" ) local json = require("json") local theTable = {} for index = 1, 150 do local value = math.random( 100 ) \< 50 and true or false theTable[index] = value end iCloud.set("theTable", json.encode(theTable)) print ("before", json.prettify(theTable)) --- theTableDownload = iCloud.get("theTable") if (theTableDownload ~= nil) then theTableDownload = json.decode(theTableDownload) print ("after", json.prettify(theTableDownload)) end

Thank you very much. I think I messed up everything. Is there a way to remove the iCloud data to start all over? I don’t see the app in the iCloud settings.

Also, I think I don’t quite understand when “initialSync” in the KVSListener is called, as I never saw it happen. Is this the point where I should set the values first to avoid overwriting them if they exist?

      • EDIT

Just to confirm, iCloud was not the cause of the broken table, it was a very unlucky combination of tables overwriting each other, too long strings, bad WiFi and my inability to understand initialSync.

Have you tried to use iCloud.delete() to delete the key first?

Also “initialSync” isn’t called unless there was an error writing the data. It’s a message stating that the data couldn’t be written due to the initial sync not being completed.

I didn’t delete the key first, I just overwrote it, that has lead to problems with the tables, because they had a different length. I just left out the KVSListener, now it works fine. It wasn’t a bug though, just a fault on my side.

Have you tried with 1 and 0 instead of true and false?

Yes, and also with strings. But my original table has more than 5 values, it has 144 x true, then 4 x false and then 1 x true again. Could that be a problem?

Edit:

I have to make a new project, it could be that I am just overwriting the table so often that I am confused with what it should look like. I will write as soon as I find out something new

@philipp3

Your code works. If I run this code it’s correct:

local iCloud = require( "plugin.iCloud" ) local json = require("json") theTable = {true, true, true, false, false} iCloud.set("theTable", json.encode(theTable)) print ("before", json.prettify(theTable)) --- theTableDownload = iCloud.get("theTable") if (theTableDownload ~= nil) then theTableDownload = json.decode(theTableDownload) print ("after", json.prettify(theTableDownload)) end

Output:

before [true,true,true,false,false] after [true,true,true,false,false]

Read the gotchas for iCould

@adrianm

But in this case he’s storing the array as a JSON string, which is perfectly fine.

@inglemar, I was referring to this in particular

Keys should be less than 64 symbols (bytes).

I would imagine a JSON string of 149 booleans would be longer that 64 bytes?