Issue with table.insert?

When I get the product information for my In-App-Purchases from Google, I put them in a sorted table using table.insert. This has worked fine in my testing on two different devices, however, I just got a Galaxy S6 and noticed that some of the products are not being displayed in my store. When I looked deeper into it, I found that those products are missing from the sorted table.

I’m really lost with this, could it be that the device or Android version affects how table.insert works? The device that works is Android 4.3, the Galaxy S6 is 5.0.2. Here is some code, and the output from the console:

local validProducts = event.products local invalidProducts = event.invalidProducts local sortedProducts = {} for k, product in pairs(validProducts) do product.title = string.gsub(product.title, "%s%(.+%)","") if shopInfo[product.productIdentifier] then local index = shopInfo[product.productIdentifier].index table.insert(sortedProducts, index, product) print("item "..index.." "..product.title) end end for k,v in pairs(sortedProducts) do print(k, v.title) end

Output from Working Device

Order of items from Google Play:

item 5 Pocketful of Coins
item 3 Increase Swaps
item 8 Big Chest of Coins
item 7 Small Chest of Coins
item 9 Wheelbarrow of Coins
item 4 Full Upgrade
item 6 Sack of Coins
item 1 Remove Ads
item 2 Infinite Lives

Sorted table:

1    Remove Ads
2    Infinite Lives
3    Increase Swaps
4    Full Upgrade
5    Pocketful of Coins
6    Sack of Coins
7    Small Chest of Coins
8    Big Chest of Coins
9    Wheelbarrow of Coins

Output from Non-Working Device

Order of items from Google Play:
(For some reason in a different order?)

item 8 Big Chest of Coins
item 1 Remove Ads
item 2 Infinite Lives
item 6 Sack of Coins
item 4 Full Upgrade
item 5 Pocketful of Coins
item 3 Increase Swaps
item 9 Wheelbarrow of Coins
item 7 Small Chest of Coins

Sorted table:
(Notice that elements 4 and 8 are missing)

1    Remove Ads
2    Infinite Lives
3    Increase Swaps
5    Full Upgrade
6    Pocketful of Coins
7    Small Chest of Coins
9    Wheelbarrow of Coins

Has anyone else experienced this issue?

I’d print out the values of product.productIdentifier.  Print out the valid and invalid products before you start working with them and see what’s going on.

Rob

Thanks, Rob. All those products were valid. I think the issue is indeed with table.insert.

I changed this line

table.insert(sortedProducts, index, product)

to this

sortedProducts[#sortedProducts + 1] = product

and now all of the items show up as expected, though not in sorted order.

I think I’m going to have to use this way and sort the table myself. I’m not sure why table.insert is failing.

Here is the output

Order of items from Google Play:

item 8 Big Chest of Coins
item 1 Remove Ads
item 2 Infinite Lives
item 6 Sack of Coins
item 4 Full Upgrade
item 5 Pocketful of Coins
item 3 Increase Swaps
item 9 Wheelbarrow of Coins
item 7 Small Chest of Coins

Unsorted items:
(They’re all there)

1    Big Chest of Coins
2    Remove Ads
3    Infinite Lives
4    Sack of Coins
5    Full Upgrade
6    Pocketful of Coins
7    Increase Swaps
8    Wheelbarrow of Coins
9    Small Chest of Coins

I’d print out the values of product.productIdentifier.  Print out the valid and invalid products before you start working with them and see what’s going on.

Rob

Thanks, Rob. All those products were valid. I think the issue is indeed with table.insert.

I changed this line

table.insert(sortedProducts, index, product)

to this

sortedProducts[#sortedProducts + 1] = product

and now all of the items show up as expected, though not in sorted order.

I think I’m going to have to use this way and sort the table myself. I’m not sure why table.insert is failing.

Here is the output

Order of items from Google Play:

item 8 Big Chest of Coins
item 1 Remove Ads
item 2 Infinite Lives
item 6 Sack of Coins
item 4 Full Upgrade
item 5 Pocketful of Coins
item 3 Increase Swaps
item 9 Wheelbarrow of Coins
item 7 Small Chest of Coins

Unsorted items:
(They’re all there)

1    Big Chest of Coins
2    Remove Ads
3    Infinite Lives
4    Sack of Coins
5    Full Upgrade
6    Pocketful of Coins
7    Increase Swaps
8    Wheelbarrow of Coins
9    Small Chest of Coins