In a Shortcut, if I have a dictionary set with default values, and then I run a set value command, does it store the value in the original dictionary or a new dictionary variable?
I ask because I have a Shortcut where I have a dictionary that I want to populate with data, let’s call it “Original Dictionary,” and then when I use Set Value, and do a Show Alert for the contents of Original Dictionary, it has not changed. But I see that the Set Value instruction outputs a dictionary and if I show Alert on that it contains all the information that I added in the Set Value. So, if we have a string of steps adding values to a dictionary, we must always use the latest output version of the Dictionary? Is that right? Does a version of the Dictionary remain in existence each step of the way throughout the life of the shortcut? Could you iterate through a dictionary enough to create the iOS version of a stack overflow? This seems weird to me.
Perplexingly you need to save the dictionary again after you set a value. Use Set Variable immediately after Set Dictionary Value.
What you’ve described is my understanding, too, and it’s bizarre. See here for similar behavior.
The behaviour has been like that forever. A dictionary is in many ways in Shortcuts a very special kind of variable, and it is noted in the set dictionary value documentation that it outputs a new dictionary.
Multiple instances of a dictionary can be accessed via magic variables throughout the processing life of a shortcut. Unless you have a massive amount of data and a massive number if loops, Shortcuts on a modern i*OS device can probably cope, but yes, with enough data and enough cycles, you could kill it, just like with any other process that generates lots of data. In the end that will come down to a combination of app memory management and designing your shortcut so that you can minimise the data overhead - set variable still has its uses for example
But that’s just the same as good design and memory management in any programming endeavour. Nothing unusual there, just good development practices will protect you
Clearly you are right and I do see that in the documentation. But, I did not quite comprehend what it meant. The documentation describes the result as “dictionary,” which is what I would expect. I did not even consider the possibility, until after testing, that it meant a new and different dictionary. It just seemed (and still seems) like bad drafting. This is particularly so, when the action offers for you to set key X to Y in a particular dictionary. Setting a value in a dictionary is not what the action actually does, it creates a new dictionary and adds a value to a particular key in the new dictionary.
I agree this requires good design, and we should all strive for good design. But certainly a system that allows you to re-use your variable would be less prone to bad design then one that spawns variables at each new step. Ultimately, you are right, though. Given the constraints we have, good design is mandatory.
But doesn’t Set Variable also just create a new variable itself? If I could do what I need with Set Variable, that would be fine.
Sorry, I think I misunderstood the real question here.
Still it’s a good question. I think if you Set a dictionary as a Variable and then overwrite that same Variable repeatedly, there’s only one explicit Variable in play. Not sure about memory use for unselected magic variables.
We desperately need a book of “design patterns” for solving problems using Shortcuts.