Save load Classes

There is an array of instances of class A. One of its properties (in class A) is an array of instances of class B

Saving to SQLite the array of A’s in table A_table
Then looping on the array of A’s, I create the records of B class in a B_table (each record has a relational code with the A instance)

When loading, first I load the A_table to fill the A’s arrays, Then loading the B_table I loop the A’s array and add it to the corresponding instance of class A.

This works, but probably could be optimized.
The question is that, for each B record read, I must loop the A’s array to find on which one I must add the record.
A better way?

[quote=495358:@Enric Herrera]There is an array of instances of class A. One of its properties (in class A) is an array of instances of class B

Saving to SQLite the array of A’s in table A_table
Then looping on the array of A’s, I create the records of B class in a B_table (each record has a relational code with the A instance)

When loading, first I load the A_table to fill the A’s arrays, Then loading the B_table I loop the A’s array and add it to the corresponding instance of class A.

This works, but probably could be optimized.
The question is that, for each B record read, I must loop the A’s array to find on which one I must add the record.
A better way?[/quote]

Use a local dictionary variable.
When loading into the A array store a dictionary entry that contains the lookup value as the key and the A class instance as the value.
When loading B you can then perform a lookup into the dictionary to find the correct A class instance.

Thanks, I’ll try it, I never used a dictionary before…

i just wrote a load/save example with json here
61108-date-storage-issue

or you could switch to something like AR (now from Strawberry Software) and basically get lazy loading for free
and the compiler can then help you out as well