Thanks for the update.
"updateChildren" throws no error - great!
For testing I used your example project and modified it a bit.
I like the way how we're working at this (do you know the movie of two guys where one is blind and the other is deaf?)
I hope we can bring this together to 100% in the next few weeks...
"updateChildren" throws no error - great!
For testing I used your example project and modified it a bit.
EDIT: This is old code. It is better to use the code from #35 below.
B4X:
#Region Project Attributes
#ApplicationLabel: FirebaseDB
#VersionCode: 2
#VersionName: px
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
'#AdditionalJar: com.google.firebase:firebase-core
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim realtime As FirebaseDatabase
Dim ref As DatabaseReference
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
realtime.Initialize("Realtime")
realtime.PersistenceEnabled = True
realtime.goOnline
Dim strTopNode As String = "testnode1"
ref.Initialize("Reference",realtime.getReferencefromUrl($"https://yyyyyyyymyidyyyyyyyy.firebaseio.com/${strTopNode}/"$) )
'ref.Initialize("Reference",realtime.getReferencefromUrl("https://tactical-patrol-603.firebaseio.com/"))
ref.addChildEventListener
ref.addListenerForSingleValueEvent
ref.addValueEventListener
'Log("ref testnode1 = " & ref.Child("testnode1"))
End Sub
Sub Activity_Resume
Testfunction_updateChildren
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Reference_onCancelled(errnum As Int,error As String)
Log($"ref_onCancelled(${errnum},${error})"$)
End Sub
Sub Reference_onChildAdded(snapshot As Object, child As String)
Log($"ref_onChildAdded(${child})"$)
End Sub
Sub Reference_onChildChanged(snapshot As Object, child As String)
Log($"ref_onChildchanged(${child})"$)
End Sub
Sub Reference_onChildMoved(snapshot As Object, child As String)
Log($"ref_onChildMoved(${child})"$)
End Sub
Sub Reference_onChildRemoved(snapshot As Object)
Log($"ref_onChildRemoved()"$)
End Sub
Sub Reference_onDataChange(snapshot As Object)
Log($"ref_onDatachange()"$)
Dim snap As DataSnapshot = snapshot
'Log("Value="&snap.Value)
Dim x As String = snap.Value
If x.Length > 0 Then
x = x.SubString2(0, Min(32, x.Length -1)) & "..."
End If
Log(" Value=" & x)
'Dim json As JSONParser
'json.Initialize(snap.Value)
'Dim root As Map = json.NextObject
'Log(root)
'Dim keys As List = snap.Children
'Log(keys.Size)
'Log(snap.Children)
End Sub
Sub Testfunction_updateChildren
' This is an extract from "Sub writeNewPost(...)" from here: https://www.b4x.com/android/forum/threads/firebase-database-rest-and-realtime-testproject.70156/
'### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
'### Sub writeNewPost(strUid As String, strUNam As String, strTitle As String, strBody As String) As Boolean
Dim strUid As String = "-KXY123"
Dim strUNam As String = "Mike Testuser"
Dim strTitle As String = "This is a testtitle"
Dim strBody As String = $"TimeStamp=${DateTime.Now} BodyText."$
'### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
Dim mapPostValues As Map
mapPostValues.Initialize
mapPostValues.Put("Uid", strUid)
mapPostValues.Put("UNam", strUNam)
mapPostValues.Put("Title", strTitle)
mapPostValues.Put("Body", strBody)
Dim strPostValues As String = raMapToJson(mapPostValues)
Log("#- strPostValues=" & strPostValues)
Dim key As String = ref.push ' Creates this --> ' https://fbdbzzzz-xyxyxyx.firebaseio.com/testnode1/-KT4Xi6oGKr0wye-zOoO ' The last part this is what we need = OK
Log("#- raw key=" & key)
If key.Length < 2 Then
Return False
End If
'
' Extract the autogenerated optimized Firebase-key
key = key.SubString(key.LastIndexOf("/") +1) '#- creates this --> -KT4Xi6oGKr0wye-zOoO
Log("#- key=" & key)
' Fanout the data to different paths --> https://firebase.googleblog.com/2015/10/client-side-fan-out-for-data-consistency_73.html
Dim strUk1 As String = "/posts/" & key
Dim strUk2 As String = "/user-posts/" & strUid & "/" & key
Log("#- strUk1=" & strUk1)
Log("#- strUk2=" & strUk2)
Dim childUpdates As Map
childUpdates.Initialize
' childUpdates.Put(strUk1, strPostValues) ' <<--str... was incorrect, it needs to be the map
' childUpdates.Put(strUk2, strPostValues )
childUpdates.Put(strUk1, mapPostValues)
childUpdates.Put(strUk2, mapPostValues)
' The JSON data that send up in a more readable format
Log("#- childUpdates=" & CRLF & raJsonPretty(raMapToJson(childUpdates )) )
ref.updateChildren(childUpdates)
End Sub
'
Sub raJsonToMap(strJSON As String) As Map
Dim jMap As Map : jMap.Initialize
Dim JSON As JSONParser : JSON.Initialize(strJSON)
jMap = JSON.NextObject
Return jMap
End Sub
Sub raMapToJson(mapX As Map) As String
Dim jsonGen As JSONGenerator
jsonGen.Initialize(mapX)
Return jsonGen.ToString
End Sub
Sub raJsonPretty(strJson As String) As String
Dim j As JSONGenerator
j.Initialize(raJsonToMap(strJson))
Return j.ToPrettyString(4)
End Sub
Sub Reference_onComplete(errorCode As Int, errorMessage As String, errObj As Object)
Log($"Reference_onComplete(...)"$)
'Log($"Reference_onComplete(${errorCode},${errorMessage})"$)
End Sub
The structure of the map in the green rectangle is the way it should look like,
the yellow framed part is what is generated by the example code.
the yellow framed part is what is generated by the example code.
I like the way how we're working at this (do you know the movie of two guys where one is blind and the other is deaf?)
I hope we can bring this together to 100% in the next few weeks...
Last edited: