Android Question Should I be able to save a B4XOrderedMap to a KeyValueStore? [=YES!]

Andris

Active Member
Licensed User
Because of the sorting advantages of a B4XOrderedMap, I'm converting my existing Map objects to B4XOrderedMap. The conversion itself is pretty straightforward. But I run into trouble when I try to save the B4XOrderedMap as an object in a KeyValueStore (v2.30).

Here's typical code for saving my Map objects to KeyValueStore, which works fine:
Map to KeyValueStore, working fine:
'(Process_Globals)

Type TRIP_DATA(id_trip As String,fid As String,wpid_start As String,wpid_dest As String,dist As Float, _
    ticks_start As Long,ticks_end As Long,size As Int,paused As Boolean)
Dim mpTrips As Map
Dim kvs_t As KeyValueStore

'(typical code)

mpTrips.Initialize
Dim trip As TRIP_DATA
trip.Initialize
mpTrips.Put("tripkey",trip)

kvs_t.Initialize(SafeDir,MainDir & "/*TRIPS")
kvs_t.Put("mpTrips",mpTrips)
kvs_t.Close
... ... ...
... and here's the converted code which results in a crash:
B4XOrderedMap to KeyValueStore, CRASHES:
'(Process_Globals)

Type TRIP_DATA(id_trip As String,fid As String,wpid_start As String,wpid_dest As String,dist As Float, _
    ticks_start As Long,ticks_end As Long,size As Int,paused As Boolean)
Dim mpTrips As B4XOrderedMap

'(typical code)

mpTrips=B4XCollections.CreateOrderedMap
'mpTrips.Initialize                                                            '(crashes whether this line is included or not)
Dim kvs_t As KeyValueStore

Dim trip As TRIP_DATA
trip.Initialize
mpTrips.Put("tripkey",trip)

kvs_t.Initialize(SafeDir,MainDir & "/*TRIPS")
kvs_t.Put("mpTrips",mpTrips)                                      '<<<<<<< this results in a CRASH!
kvs_t.Close
... ... ...
This is what the Debugger Log shows when it crashes:
Error occurred on line: 18 (KeyValueStore)
java.lang.RuntimeException: Cannot serialize object: [b4xcollections=null, general=null, httputils2service=null
, list=(ArrayList) [test], main=null, map=(MyMap) {test=[IsInitialized=true, dist=0.0, fid=
, id_trip=, paused=false, size=0
, ticks_end=0, ticks_start=0, wpid_dest=
, wpid_start=]}

It looks to me like KeyValueStore isn't compatible (yet?) with B4XOrderedMap. Or am I leaving out some necessary code?
 

Nicola Ciaramellano

Member
Licensed User
Hi,
I tested your code adding the get procedure just for checking in B4j and it works without errors

B4X:
Dim mpTrips As B4XOrderedMap
    '(typical code)

    'mpTrips=B4XCollections.CreateOrderedMap
    mpTrips.Initialize                                                            '(crashes whether this line is included or not)
    Dim kvs_t As KeyValueStore
    kvs_t.Initialize(File.DirData("Appname"),"datastore")
    Dim trip As TRIP_DATA
    trip.Initialize
    trip.dist=1.2
    trip.fid="Test fid"
    mpTrips.Put("tripkey",trip)
    kvs_t.Put(1,mpTrips.Get("tripkey"))
    Dim testtrips As B4XOrderedMap
    testtrips.Initialize
    testtrips.Put("test",kvs_t.Get(1))
    Dim testtipdata As TRIP_DATA
    testtipdata.Initialize
    testtipdata=testtrips.Get("test")
    Log("Dist: " & testtipdata.dist & " Fid: " & testtipdata.fid)

I think that in b4i and b4a it's the same...
 
Upvote 0

DonManfred

Expert
Licensed User
Upvote 0

Andris

Active Member
Licensed User
Hi,
I tested your code adding the get procedure just for checking in B4j and it works without errors

B4X:
Dim mpTrips As B4XOrderedMap
    '(typical code)

    'mpTrips=B4XCollections.CreateOrderedMap
    mpTrips.Initialize                                                            '(crashes whether this line is included or not)
    Dim kvs_t As KeyValueStore
    kvs_t.Initialize(File.DirData("Appname"),"datastore")
    Dim trip As TRIP_DATA
    trip.Initialize
    trip.dist=1.2
    trip.fid="Test fid"
    mpTrips.Put("tripkey",trip)
    kvs_t.Put(1,mpTrips.Get("tripkey"))
    Dim testtrips As B4XOrderedMap
    testtrips.Initialize
    testtrips.Put("test",kvs_t.Get(1))
    Dim testtipdata As TRIP_DATA
    testtipdata.Initialize
    testtipdata=testtrips.Get("test")
    Log("Dist: " & testtipdata.dist & " Fid: " & testtipdata.fid)

I think that in b4i and b4a it's the same...
Thanks for taking the time to try it Nicola. I'll keep looking into it.
 
Upvote 0

Andris

Active Member
Licensed User
So far it looks like you cannot save a B4XOrderedMap as a value object in a KeyValueStore. Wonder if there's a way around this other than going back to my Map objects and then reading into B4XOrderedMap when needed. Maybe a future update of KeyValueStore?
 
Upvote 0

Andris

Active Member
Licensed User
B4X:
Dim b() As Byte = ser.ConvertObjectToBytes(OrderedMap1.GetDataForSerializator)
kvs.Put("bbb", b)
Dim NewMap As B4XOrderedMap
NewMap.Initialize
NewMap.SetDataFromSerializator(ser.ConvertBytesToObject(kvs.Get("bbb")))

Erel, you never fail to give us mere humans something to aspire to. Always think out of the box! Thank you!
 
Upvote 0
Top