skip to Main Content

I am writing a todolist application
there are two screens
MainVC – table
SecondVC – with two testfeeds where the user writes notes

I save when I click on the button

private var databaseRef = Database.database().reference()

ActionButton

let user = Auth.auth().currentUser   
self.databaseRef.child((user?.uid)!).child("tasklist").childByAutoId().setValue(["textPrimary":textPrimary,                                                                     "textSecondary":textSecondary])

I use childByAutoId() because a user can have many notes
Example my database:

GcOialHBMfWxV9AgUJXR4zUsf603 =     { // user.uid
        tasklist =         { // child("tasklist")
            "-N9km0vd6W_gs3ljMRyw" =             { // .childByAutoId()
                textPrimary = 123; //setValue(["textPrimary":textPrimary,
                textSecondary = Tasktask; //  "textSecondary":textSecondary]
            };
            "-N9km4EMruNUvSDsCCAY" =             { // .childByAutoId()
                textPrimary = OtherTask;
                textSecondary = Taskkkkk;
            };
        };
    };

How do I get the key of an already created notes?(example -N9km0vd6W_gs3ljMRyw)
I load the data like this

    // load data in first VC
 let user = Auth.auth().currentUser
    var ref = databaseRef.child("(user!.uid)").child("tasklist").childByAutoId()
    print(ref)
        ref.observeSingleEvent(of: .value, with: { (snapshot) in
             print(snapshot.value)

        })
}

.childByAutoId() constantly creates a new key (which is not in the database, and I need a key from the database)
Or do I have the wrong approach? Then what should I do?

2

Answers


  1. I am sorry if this answer would better a comment, but I wanted to add a code snippet.

    From the firebase documentation:

    childByAutoId generates a new child location using a unique key and
    returns a FIRDatabaseReference to it

    So as far as I understood, you get the id from the reference (in your example ref) and I could imagine it looks like that:

    // create ref, based on childByAutoId()
    var ref = FIRDatabase.database().reference().child("tasks").childByAutoId()
    
    // add value
    ref.setValue(task)
    
    // get the id from the reference
    let childautoID = ref.key 
    
    // let's see the id in the logs
    print(childautoID)
    
    Login or Signup to reply.
  2. The snapshot object in your last code snippet is of type DataSnapshot. In addition to having a value, it also has a key – which is the string generated when you call childByAutoId.

    So:

    ref.observeSingleEvent(of: .value, with: { (snapshot) in
        print(snapshot.key) // 👈
        print(snapshot.value)
    })
    

    If you load the entire tasklist node, you can loop over the children of the snapshot (whose keys you won’t know) as shown in the Firebase documentation on getting a list of nodes by listening for a value event:

    var tasklistRef = databaseRef.child("(user!.uid)").child("tasklist")
    tasklistRef.observe(.value) { snapshot in
      for taskSnapshot in snapshot.children {
        ...
      }
    }
    

    To learn more about this, also see:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search