Android Question Firebase Realtime with crash only on write code

amorosik

Expert
Licensed User
I am trying to run a basic program for reading and writing on Firebase Realtime
I use an app with Main and Starter module as described below
The initial reading of the db and its variations are correctly displayed on the log window
When I try to write, by pressing one of the two buttons on the only form available (calling writeDB sub on Starter module), the program crashes
And I don't understand exactly where the blocking occurs
Is the writing routine correct at the code level?
What could app lock depend on?
B4A 10.0, Firebase Realtime 2.82, Core 9.90, Phone 2.50, XUI 2.00


code of Main module:
#Region  Project Attributes
    #ApplicationLabel: Firebase Realtime DB
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #BridgeLogger: True
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

#AdditionalJar: com.google.firebase:firebase-database

Sub Process_Globals
    Private xui As XUI
End Sub

Sub Globals
    Private txtCampo As EditText
    Private txtValore As EditText
    Private txtCampo2 As EditText
    Private txtValore2 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("layout")
    txtCampo.text="Comando"
    txtCampo2.Text="Comando"
    txtValore.Text="123456"
    txtValore2.Text="ABCDE"
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub cmdInvia_Click
    Starter.writeDB( txtCampo.Text,txtValore.Text)
End Sub

Sub cmdInvia2_Click
    Starter.writeDB( txtCampo2.Text,txtValore2.Text)
End Sub

code of Starter module:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Dim realtime As FirebaseDatabase
    Dim msgref As DatabaseReference
    Dim qry As FBQuery
    Public phone As Phone
End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
End Sub

Sub Service_Start (StartingIntent As Intent)
    realtime.Initialize("Realtime")
    realtime.PersistenceEnabled = True
    realtime.goOnline

    msgref.Initialize("Reference",realtime.Reference.Child("ERP2/1234567890"),"ERP2/1234567890")

    msgref.addChildEventListener
    msgref.addListenerForSingleValueEvent
    msgref.addValueEventListener
    Log("msgref imagesizes = " & msgref.Child("imagesizes"))

    qry.Initialize("QueryResult",msgref.orderByChild("comando").QueryObj)
    qry.limitToFirst(30).addListenerForSingleValueEvent
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy
End Sub

#Region Query Result
Sub QueryResult_onDataChange(snapshot As Object, tag As Object)
    Log($"QueryResult_onDataChange()"$)

    If snapshot <> Null Then
        Dim snap As DataSnapshot
        snap.Initialize(snapshot)
        'Log("Snapshot value is NOT null. Ok. Need to check content")
        If snap.Value Is Map Then
            'Log("Value type Map: "& GetType(snap.Value))
            Dim m As Map = snap.Value
            Log(m)
            For Each k As String In m.Keys
                Log(m.Get(k))
            Next
          
            'fbdb = snap.Value
            'Log("FBDBroot set...")
        else if snap.Value Is List Then
            'Log("Value type List: "& GetType(snap.Value))
            Dim customers As List = snap.Value
            Log("referenceListSize: "&customers.Size)
        else if snap.Value Is String Then
            'Log("Value type String: "& GetType(snap.Value))
            Dim val As String = snap.Value
            Log("Value="&val)
        Else
            Log("Value type unknown: "& GetType(snap.Value))
        End If

    Else
        Log("Snapshot is null")
    End If
      
End Sub
#End Region

#Region FBReference
Sub Reference_onChildAdded(snapshot As Object, child As String, tag As Object)
    Log($"Reference_onChildAdded(${child},${tag})"$)
    Dim snap As DataSnapshot
    snap.Initialize(snapshot)
    Select child
        Case "options"
            'Dim options As Map = snap.Value
            'Log("-------------------")
            'Log("--- OPTIONS -------")
            'Log(options)
            'Log("-------------------")
        Case "geoFire"
        Case "users"
            'Dim users As Map = snap.Value
            'Log("-------------------")
            'Log("--- USERS ---------")
            'Log(users)
            'Log("-------------------")
    End Select
    'Log("-------------------")
    'Log(snap.Key)
    'Log(snap.Value)
    'Log("---_onChildAdded()")
End Sub

Sub Reference_onChildChanged(snapshot As Object, child As String, tag As Object)
    Log($"Reference_onChildchanged(${child},${tag})"$)
End Sub

Sub Reference_onComplete(resultCode As Int, resultMessage As String, result As Object, tag As Object)
    Log($"Reference_onComplete(${resultCode},${resultMessage},${tag})"$)
End Sub

Sub Reference_onChildMoved(snapshot As Object, child As String, tag As Object)
    Log($"Reference_onChildMoved(${child},${tag})"$)
End Sub

Sub Reference_onChildRemoved(snapshot As Object, tag As Object)
    Log($"Reference_onChildRemoved(${tag})"$)
End Sub

Sub Reference_onDataChange(snapshot As Object, tag As Object)
    Log($"Reference_onDatachange(${tag})"$)
    If snapshot <> Null Then
        Dim snap As DataSnapshot
        snap.Initialize(snapshot)
        'Log("Snapshot value is NOT null. Ok. Need to check content")
        If snap.Value Is Map Then
            'Log("Value type Map: "& GetType(snap.Value))
            Dim m As Map = snap.Value
            Log(m)
            For Each k As String In m.Keys
                Log(m.Get(k))
            Next
          
            'fbdb = snap.Value
            'Log("FBDBroot set...")
        else if snap.Value Is List Then
            'Log("Value type List: "& GetType(snap.Value))
            Dim customers As List = snap.Value
            Log("referenceListSize: "&customers.Size)
        else if snap.Value Is String Then
            'Log("Value type String: "& GetType(snap.Value))
            Dim val As String = snap.Value
            Log("Value="&val)
        Else
            Log("Value type unknown: "& GetType(snap.Value))
        End If

    Else
        Log("Snapshot is null")
    End If

    Dim keys As List = snap.Children
    Log(keys.Size)
    Log(snap.Children)
      
End Sub
#End Region

Sub writeDB(campo As String, valore As String)
    Dim mp As Map:    mp.Initialize
    mp.Put(campo,valore)
    msgref.push
    msgref.updateChildren(mp)
End Sub

code of Manifest:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="28"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.



'************ Google Play Services Base ************
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
'************ Google Play Services Base (end) ************
'************ Firebase Base ************
CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
'************ Firebase Base (end) ************
'************ Firebase Notifications ************
CreateResourceFromFile(Macro, FirebaseNotifications.FirebaseNotifications)
'************ Firebase Notifications (end)************
'************ Firebase Auth ************
CreateResourceFromFile(Macro, FirebaseAuth.FirebaseAuth)
'************ Firebase Auth (end) ************
AddPermission(android.permission.INTERNET) ' Allows applications to open network sockets.
AddPermission(android.permission.ACCESS_NETWORK_STATE) ' Allows applications to access information about networks.

Immagine.png


windows LOG:
lib3:Raising.. reference_onchildchanged()
lib1:Raising.. reference_ondatachange()
Reference_onChildchanged(Flag_Eseguito,ERP2/1234567890)
Reference_onDatachange(ERP2/1234567890)
(MyMap) {Tempo_Data=2020/09/22, Telefono=ddddaaaaaaaaaaaaaaaaaaaaa, Autista=0, Mittente_Ip_Address=192.168.1.180, Root=ERP2, comando=5556, Tempo_Ora=08:54:28, Comando=1234567, Id_Comando=aaaaaaaaaaa, Flag_Eseguito=0}
2020/09/22
ddddaaaaaaaaaaaaaaaaaaaaa
0
192.168.1.180
ERP2
5556
08:54:28
1234567
aaaaaaaaaaa
0
lib3:Raising.. reference_onchildchanged()
lib1:Raising.. reference_ondatachange()
Reference_onChildchanged(Autista,ERP2/1234567890)
Reference_onDatachange(ERP2/1234567890)
(MyMap) {Tempo_Data=2020/09/22, Telefono=ddddaaaaaaaaaaaaaaaaaaaaa, Autista=0, Mittente_Ip_Address=192.168.1.180, Root=ERP2, comando=5556, Tempo_Ora=08:54:28, Comando=123456, Id_Comando=aaaaaaaaaaa, Flag_Eseguito=0}
2020/09/22
ddddaaaaaaaaaaaaaaaaaaaaa
0
192.168.1.180
ERP2
5556
08:54:28
123456
aaaaaaaaaaa
0
Error occurred on line: 0 (Starter)
java.lang.Exception: Sub reference_oncomplete signature does not match expected signature.
public static anywheresoftware.b4a.pc.RemoteObject com.erp2.firebaserealtime.starter_subs_0._reference_oncomplete(anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject) throws java.lang.Exception

class anywheresoftware.b4a.pc.RemoteObject, class anywheresoftware.b4a.pc.RemoteObject, class anywheresoftware.b4a.pc.RemoteObject,
 
Last edited:
Top