Android Question KVS-problem

Herbert32

Active Member
Licensed User
Longtime User
Hi,

during changing my code to store data asynchronous in KVS it seems that I destroyed the content of a Map with 1808 entries.

To see what happened exactly, i would like to take a look inside the stored data but seems to be not so easy...

In the KeyValueStore-Class 'getObjectInternal' the data is read into the Object 'res' - I'm able to view the first 100 entries with the debugger but I need to see the last 20 to know more what happened.

It's possible to pass the object 'res' to my Map but as soon as I access the Map, my app crashes also in debug-mode.

It's important for my further development to get the intact data back but right now, i have no idea how to reach this...

Maybe someone can push me into the right direction.

thank you in advance

Herbert
 

Herbert32

Active Member
Licensed User
Longtime User
sorry, here:

B4X:
Error occurred on line: 565 (Starter)
java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap
    at anywheresoftware.b4a.objects.collections.Map.getSize(Map.java:111)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at order.star.pro.starter._kvsread(starter.java:916)
    at order.star.pro.starter._service_create(starter.java:1521)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at order.star.pro.starter.onCreate(starter.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2895)
    at android.app.ActivityThread.access$1900(ActivityThread.java:157)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1439)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5551)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
 
Upvote 0

Herbert32

Active Member
Licensed User
Longtime User
also the possibility to remove the broken or defect entries starting from the last in descending order will be ok - this gives me the chance to receive my important data back and check by debugging while saving new data in the new coded way what i'm doing wrong.
 
Upvote 0

Herbert32

Active Member
Licensed User
Longtime User
B4X:
If KVS.ContainsKey("CardTrans") Then
    mCardTrans = KVS.GetObject("CardTrans")
Else
    mCardTrans.Clear
    KVS.PutObject("CardTrans",mCardTrans)
End If
NextTransID = mCardTrans.Size+1

it crashes in the line
B4X:
    NextTransID = mCardTrans.Size+1

it also crashes when i set a breakpoint to that line and try to move the cursor over 'mCardTrans' in this line
B4X:
        mCardTrans = KVS.GetObject("CardTrans")
to let the debugger show me mCardTrans's content in the contextmenue
 
Upvote 0

Herbert32

Active Member
Licensed User
Longtime User
hmmm...

this is strange...

by this way, I can fill my Map

B4X:
    mCardTrans = m
after that all works as expected...
 
Upvote 0

Herbert32

Active Member
Licensed User
Longtime User
hmmm - how to find out now why

B4X:
Dim jo As JavaObject = KVS.GetObject("CardTrans")
log(jo)
Dim m As Map = jo.RunMethod("getObject", Null)
mCardTrans = m

works while
B4X:
    mCardTrans = KVS.GetObject("CardTrans")

doesn't work ???
 
Upvote 0

Herbert32

Active Member
Licensed User
Longtime User
I went back now to a backup which I created before my last changes to be sure that I did not changed something accidently and find the same situation - cant read on the original way

But by doing this I found another strange behaviour also:

this is my original code in the KeyValueStore-Class for getObjectInternal:
B4X:
Private Sub getObjectInternal(Key As String, decrypt As Boolean, password As String) As Object
    Dim c As Cursor = getCursor(Key)
    If c.RowCount = 0 Then
        c.Close
        Return Null
    End If
    c.Position = 0
    Dim buffer() As Byte = c.GetBlob2(0)
    Dim raf As RandomAccessFile
    raf.Initialize3(buffer, False)
    Dim res As Object
    If decrypt Then
        res = raf.ReadEncryptedObject(password, raf.CurrentPosition)
    Else
        res = raf.ReadObject(raf.CurrentPosition)
    End If
    raf.Close
    c.Close
    Return res
End Sub
'Return res' is located in Line 179 and I receive following Log while Debug-Mode:

B4X:
*** Service (starter) Create ***
Error occurred on line: 179 (KeyValueStore)
java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap
    at order.star.pro.starter._kvsread(starter.java:1256)
    at order.star.pro.starter._service_create(starter.java:1472)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at order.star.pro.starter.onCreate(starter.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2895)
    at android.app.ActivityThread.access$1900(ActivityThread.java:157)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1439)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5551)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
** Service (starter) Start **

If I add a Line for Logging before the 'Return res', 'Return res' moves to Line 180:
B4X:
Private Sub getObjectInternal(Key As String, decrypt As Boolean, password As String) As Object
    Dim c As Cursor = getCursor(Key)
    If c.RowCount = 0 Then
        c.Close
        Return Null
    End If
    c.Position = 0
    Dim buffer() As Byte = c.GetBlob2(0)
    Dim raf As RandomAccessFile
    raf.Initialize3(buffer, False)
    Dim res As Object
    If decrypt Then
        res = raf.ReadEncryptedObject(password, raf.CurrentPosition)
    Else
        res = raf.ReadObject(raf.CurrentPosition)
    End If
    raf.Close
    c.Close
    Log(Key)
    Return res
End Sub

But look what now happens in my Log:

B4X:
*** Service (starter) Create ***
ToBeUpdated
DeviceAdminLock
NoLockCredit
MC_Created
NoLockSession
NoLockTime
CheckSum1Ignore
DataWAGRU
mArtikel
DataETC
Cards
Error occurred on line: 180 (Starter)
java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap
    at order.star.pro.starter._kvsread(starter.java:1256)
    at order.star.pro.starter._service_create(starter.java:1472)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at order.star.pro.starter.onCreate(starter.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2895)
    at android.app.ActivityThread.access$1900(ActivityThread.java:157)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1439)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5551)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
** Service (starter) Start **

The Error occures now in Line 180 as expected - But the Log does not show 'KeyValueStore' as Module where it occours - it shows 'Starter' - one of both is false - Line-# or Module ...


but MOST curious is what happens if I set a Breakpoint in my Sub in the starter-service where I read all my Values which I stored in KVS exactly AFTER passing the content to my first 'problem-Map'...

the curiosity is that 'NO Error happens' - it only happens when the Map is touched the first time by getting it's size...

code-snippet:
B4X:
    If KVS.ContainsKey("Cards") Then
        mCards = KVS.GetObject("Cards")
    Else
        mCards.Clear
        KVS.PutObject("Cards",mCards)
    End If
    NextCardID = mCards.Size+1

    If KVS.ContainsKey("EProt") Then
        EProt = KVS.GetObject("EProt")
    Else
        EProt.Clear
        KVS.PutObject("EProt",EProt)
        ToBeUpdated = True
    End If

Log if setting a breakpoint to Line 572 'If KVS.ContainsKey("EProt") Then':
B4X:
Copying updated assets files (32)
*** Service (starter) Create ***
ToBeUpdated
DeviceAdminLock
NoLockCredit
MC_Created
NoLockSession
NoLockTime
CheckSum1Ignore
DataWAGRU
mArtikel
DataETC
Cards
Error occurred on line: 570 (Starter)
java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap
    at anywheresoftware.b4a.objects.collections.Map.getSize(Map.java:111)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at order.star.pro.starter._kvsread(starter.java:916)
    at order.star.pro.starter._service_create(starter.java:1472)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at order.star.pro.starter.onCreate(starter.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2895)
    at android.app.ActivityThread.access$1900(ActivityThread.java:157)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1439)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5551)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

Hopefully I was able to describe all the curiosities which are happen here...

I noticed in the past that sometimes line# and module-name doesn't match in the debugging-log - but never had been able to reproduce so clear than here

But all doesn't really help - i can't find why my original code doesnt work anymore...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The Error occures now in Line 180 as expected - But the Log does not show 'KeyValueStore' as Module where it occours - it shows 'Starter' - one of both is false - Line-# or Module ...
This doesn't matter.

Somehow you got a wrapped Map into the KVS. I'm not sure how it happened. The code I posted unwraps it.
 
Upvote 0
Top