B4J Question How to save and restore objects properties ?

FrenchDeveloper

Member
Licensed User
Longtime User
Hello,
I have a set of objects (class). Each object is an imageview + properties.
This set will grow without limit. Properties number will grow also and may be some of them will be deleted.
What is the best solution to save and restore these objects properties ? I would like to avoid either iterations or duplication of properties in a separate collection.

I tried 2 approaches :
- WriteMap : writes all necessary properties in the file testmap but I cannot restore them

- KeyValueStore : doesn't write properties in the file testkvs (probably because my objects contain not primitive values)

Thank you in advance for your replies.
 

Attachments

  • Map save.zip
    92.7 KB · Views: 281
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't pass the Items list to the class instance. It is not the "responsibility" of the object to add itself to the collection. Instead create the object and add it to the list.

I would have used something like:
B4X:
Public Sub Initialize
   Icon.Initialize("Icon")
End Sub

Public Sub CreateNew( ID_v As Int, Group_v As String, Image_file_v As String)
  If Image_file_v.Length <> 0 Then
     Icon.SetImage(fx.LoadImage(File.DirApp & "/Images",Image_file_v))
     Image_file = Image_file_v
   End If
   ID = ID_v
   Group = Group_v
End Sub

Public Sub CreateFromMap(m As Map)
   Dim Image_file_v As String = m.Get("image_file")
   Dim Group_v As String = m.Get("group")
   Dim ID_v As Int = m.Get("id")
   CreateNew(ID_v, Group_v, Image_file_v)
End Sub

Public Sub ExportToMap As Map
   Dim m As Map
   m = CreateMap("image_file": Image_file, "id": ID, "group": Group)
   Return m
End Sub
Now when you want to save an object you call ExportToMap and write this map to KVS (probably better to add all objects to a list to a list and then write the list).

Later you read the list of maps and go over it and create the objects.
 
Upvote 0

FrenchDeveloper

Member
Licensed User
Longtime User
Hello Erel, Thanks for the quick reply !
If I understand correctly,
- The structure to save will be : List - Map - Properties
- I need to iterate among all objects to save them. That's what I expected to avoid because of performance issues. The number of objects may be high and the user will need to wait for all objects to be saved when he will want to save only one item.

I updated the project in the first post including images folder. Here are performance results for 6 objects :
WriteMap : 62 ms
KVS list-map PutObject : 656 ms
KVS list-map GetObject : 157 ms
KVS map-map PutObject : 968 ms
KVS map-map GetObject : 172 ms
KVS map-type PutObject : 968 ms
KVS map-type GetObject : 172 ms

Conclusions :
- WriteMap : 10ms by object to save / impossible to restore / impossible to update one object at a time
- KVS list-map : 100ms by object to save / 25ms by object to restore / impossible to update one object at a time
- KVS map-map : 150ms by object to save / 30ms by object to restore / possible to update one object at a time
- KVS map-type : 150ms by object to save / 30ms by object to restore / possible to update one object at a time

If this analysis is correct, I will better choose KVS map-type approach because of the possibility to update objects instead to save all the objects.
 
Upvote 0

FrenchDeveloper

Member
Licensed User
Longtime User
My computer is rather slow but relative performance is more important for me to choose an approach. Here are performances without initializing kvs :
WriteMap : 63 ms
KVS list-map PutObject : 187 ms
KVS list-map GetObject : 156 ms
KVS map-map PutObject : 797 ms
KVS map-map GetObject : 188 ms
KVS map-type PutObject : 797 ms
KVS map-type GetObject : 203 ms

Conclusions :
- WriteMap : 10ms by object to save / impossible to restore / impossible to update one object at a time
- KVS list-map : 30ms by object to save / 25ms by object to restore / impossible to update one object at a time
- KVS map-map : 130ms by object to save / 30ms by object to restore / possible to update one object at a time
- KVS map-type : 130ms by object to save / 30ms by object to restore / possible to update one object at a time

Project is updated in first post
 
Upvote 0
Top