UUID usage in Demystify SwiftUI video

At about 33:30 of the Demystify SwiftUI video, it indicates the problem is the id isn't stable. Since it is a computed property, the UUID() function is called each time the property is accessed and a different value is returned. The simple fix is to instead use let id = UUID() isn't it or am I still misunderstanding the problem?

Thanks, Dave

Replies

My thought, exactly.

In some cases I would expect that would resolve the issue, especially in simple cases like demo apps. However that assumes that the structs are not recreated.

However that won't work in more complex cases. For example if you get that data from your server reloading would generate a new object with a new UUID. That is why they recommend long life ids, like database ids, which would persist through operations like reloading from your server.

The use case I’m thinking of is creating these structures locally and then storing it (either in JSON or Core Data) so I can use the UUID to get an identifier and then store it so that it does persist with the data later but I understand the point you’re making @iressler. For Core Data you might be able to use the objectID, but I have a vague recollection that the objectID can change between initial creation and when it is stored to the persistent store (i.e., SQLite).