1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question FireStore Edit Field document

Discussion in 'Android Questions' started by gregorio_adrian_gimenez, Jun 1, 2019.

Tags:
  1. gregorio_adrian_gimenez

    gregorio_adrian_gimenez Active Member Licensed User

    Hi,


    I would need an example of the code to update the data of a field of a document, could you help me?
     
  2. gregorio_adrian_gimenez

    gregorio_adrian_gimenez Active Member Licensed User

    For example Field Nombre

    upload_2019-6-1_0-25-58.png
     
  3. DonManfred

    DonManfred Expert Licensed User

    - 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
    Code:
    Dim m As Map = CreateMap("Nombre""gabriela","Alias":"gabriela","Areas":"ingeneria")
        users.add(m)
    Code:
    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
    Code:
    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")
     
  4. DonManfred

    DonManfred Expert Licensed User

    Note that you should NOT post such big images directly in the post.
    Better upload the image and post a thumbnail...

    [​IMG]
    [​IMG]

    ->>>> Snap14.png
     
  5. gregorio_adrian_gimenez

    gregorio_adrian_gimenez Active Member Licensed 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

    Code:
    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)
     
  6. DonManfred

    DonManfred Expert Licensed User

    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.

    Code:
    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: Jun 2, 2019
  7. gregorio_adrian_gimenez

    gregorio_adrian_gimenez Active Member Licensed 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
     
  8. gregorio_adrian_gimenez

    gregorio_adrian_gimenez Active Member Licensed User

    This code works very well. Thanks every time the GPS position changes, update the coordinates in firestore!

    Code:
    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: Jun 2, 2019
    DonManfred likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice