We have a tree-based data model that is based around the Codable protocol. The root of the tree holds its immediate children, as well as a reference to all children in the hierarchy, as seen here...
Root
|
|-->Children
| |
| |-->Item 1
| |-->Item 2
| | |
| | \-->Children
| | |
| | \-->Item 3
| |
| \-->Item 4
| |
| \-->Children
| |
| \-->Item 5
| |
| \-->Children
| |
| \-->Item 6
|
\-->AllChildren <<-- Not Serialized!!
|
|-->Item 1
|-->Item 2
|-->Item 3
|-->Item 4
|-->Item 5
\-->Item 6
Now the AllChildren part isn't serialized as they are just references to the actual instances from above.
To make the above work, we need to populate AllChildren programmatically, as those children are being decoded. However, we aren't sure how to pass the Root object into the children's init(from:Decoder) calls to handle that as we don't see any way to pass additional state data into the decoding chain. It seems the only information you have available is the Decoder, which you don't control.
Our work-around is inside the init(from:Decoder) of the Root, once it's done decoding/initializing all of its children, it then re-crawls the entire hierarchy, slurping up the children, but I really hate that I have to re-crawl the hierarchy after just doing it during the init(from:Decoder) pass.
So is there a way to pass additional state information into the Decode process, preferably as something on the Decoder handed to the init(from:Decoder) calls?