Android Question FireStore Edit Field document

gregorio_adrian_gimenez

Active Member
Licensed User
Longtime User
For example Field Nombre

upload_2019-6-1_0-25-58.png
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
example of the code to update the data of a field of a document
- get v0.61 of library (same Dropboxlink).
- You need to have a DocumentReference for the Docuent you want to change
- Replace the data with a new map of data or merge a set of new/changed data

Insert new Set into Collection
B4X:
    Dim m As Map = CreateMap("Nombre": "gabriela","Alias":"gabriela","Areas":"ingeneria")
    users.add(m)

B4X:
        Dim ref As DocumentReference = value ' you need to have this already
        ref.setEventname("UserWatch","Update")
        Log("DocumentReferenceId: "&ref.Id)
        Dim m As Map = CreateMap("Nombre": "gabrielanew")
        ref.merge(m,"update") ' add Nombre field to document replacing the existing Field.

or replace the complete set
B4X:
        Dim ref As DocumentReference = value
        ref.setEventname("UserWatch","Update")
        Log("DocumentReferenceId: "&ref.Id)
        Dim m As Map = CreateMap("Nombre": "gabrielanew","Alias":"gabriela","Areas":"ingeneria")
        ref.update(m,"update")
 
Upvote 0

gregorio_adrian_gimenez

Active Member
Licensed User
Longtime User
thanks Don!

Sorry, but how do I do this? You need to have a DocumentReference for the Docuent you want to change


my code

B4X:
Public Sub ubicacionmodificada(Location1 As Location) ' gps actualizacion de ubicacion
    fs.Initialize("Firestore",True)
    loc.Initialize(Location1.Latitude,Location1.Longitude)
    
    Dim usuarios As CollectionReference= fs.collection("usuarios","usuarios_coleccion")
    If Starter.auth.CurrentUser.IsInitialized Then
            
    If usuarios <> Null Then
'            usuarios.setEventname("resultadovalidaciondatosusuario","Userlist")
            
        usuarios.whereEqualTo("usuario_Uid",Starter.auth.CurrentUser.Uid).limit(25).fetch("usuarioscambiogps")
        
    
    End If
    End If
    
End Sub

Sub    usuarioscambiogps_snapshot(success As Boolean, data As QuerySnapshot, info As String)
    fs.Initialize("Firestore",True)
    If data.Size > 0 Then
        
    Log ("usuario cambio gps:" & success)
        
        
    Dim documento As List = data.Documents
        
        Dim cambio As DocumentReference
    If documento.IsInitialized Then
        'If  documento > 0 Then
            For i = 0 To documento.Size-1
                Dim docsnap As DocumentSnapshot = documento.Get(i)
                
                    
                Dim cambio_gps As DocumentReference
                Dim ref As DocumentReference = data.Documents ' you need to have this already
                ref.setEventname("UserWatch","Update")
                Log("DocumentReferenceId: "&ref.Id)
                Dim m As Map = CreateMap("Nombre": "gabrielanew")
                ref.merge(m,"update") ' add Nombre field to document replacing the existing Field.
            Next
        
    End If
    End If
End Sub

Generated this error
main_usuarioscambiogps_snapshot (java line: 981)
java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.google.firebase.firestore.DocumentReference
at com.aeropuerto.myapp.main._usuarioscambiogps_snapshot(main.java:981)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.BA$2.run(BA.java:370)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Sorry, but how do I do this?
Sorry, but learn to work with this lib. Make sure to consult the firebasedocumentation. I can not help you with every question you have.

If you are facing any error then create a new thread for sure.

B4X:
    Dim usr As DocumentReference = fs.document("Users/sYckIxPxDDsuV5m6k2Hr") ' This is the userid in MY Database for sure. Use the correct id from your database!
    usr.setEventname("UserWatch","User")
 
Last edited:
Upvote 0

gregorio_adrian_gimenez

Active Member
Licensed User
Longtime User
I'm going to think about quitting using B4A, it does not help much for beginners, I do not want them to get mad because I ask.
Thank you very much Don for your help,
I will try to ask less
 
Upvote 0

gregorio_adrian_gimenez

Active Member
Licensed User
Longtime User
This code works very well. Thanks every time the GPS position changes, update the coordinates in firestore!

B4X:
Public Sub ubicacionmodificada(Location1 As Location) ' gps actualizacion de ubicacion
    fs.Initialize("Firestore",True)
    loc.Initialize(Location1.Latitude,Location1.Longitude)
    
    Dim usuarios As CollectionReference= fs.collection("usuarios","usuarios_coleccion")
    If Starter.auth.CurrentUser.IsInitialized Then
            
    If usuarios <> Null Then
'            usuarios.setEventname("resultadovalidaciondatosusuario","Userlist")
            
        usuarios.whereEqualTo("usuario_Uid",Starter.auth.CurrentUser.Uid).limit(25).fetch("usuarioscambiogps")
        
    
    End If
    End If
    
End Sub

Sub    usuarioscambiogps_snapshot(success As Boolean, data As QuerySnapshot, info As String)
    fs.Initialize("Firestore",True)
    If data.Size > 0 Then
        
    Log ("usuario cambio gps:" & success)
        
        
    Dim documento As List = data.Documents
        

    
    If documento.IsInitialized Then
        'If  documento > 0 Then
            For i = 0 To documento.Size-1
                Dim docsnap As DocumentSnapshot = documento.Get(i)
            
                Log("consulta Documento No: #"&i&": "&docsnap.Id)
        
                
                    
                Dim cambio_gps As DocumentReference =fs.document("usuarios/"& docsnap.Id)
        
                
                cambio_gps.setEventname("actualizausuariosgps","Update")
            
                
                Dim geo As Map
                geo.Initialize
                geo.Put("geo",loc)
                
                cambio_gps.merge(geo,"update")
            Next
        
    End If
    End If
End Sub
 
Last edited:
Upvote 0
Top