Set a childByAutoId value in Firebase data service class to be retrieved in another class

Question!

Hopefully I can make this more clear than my title. I created a FirebaseService class. In it I will create all of the methods for saving and retrieving data. I don't want to have to make a firebase reference in any other class. I simply want to reference this class to do that. If this isn't possible, please let me know so I don't move forward with it. Below is my FirebaseService class...

import UIKit
import Firebase

class FirebaseService: NSObject {

    let ref = FIRDatabase.database().reference()
    var id = String()


    func savePost(post:[String:AnyObject]) {
        let postRef = ref.child("posts").childByAutoId()
        id = postRef.key
        postRef.setValue(post)

    }



}

I want to be able to retrieve id from the FirebaseService class and have it actually use the value of childByAutoId (the post's randomly generated id in Firebase) when I save a post so I can save it as a value in post called "id". However, right now when I save it, I get an intended random id generated for the post as a whole but I don't get that value in my "id" key.

func postPost() {
    post.id = FirebaseService().id
    post.textContent = textView.text
    post.name = "Michael Williams"
    post.profileImageName = "cool"
    post.imageContentName = "coffee"
    post.createdDate = convertCurrentTimeToString()
    let postArray:[String:AnyObject] = ["name":post.name!, "profileImageName":post.profileImageName!, "textContent":post.textContent!, "imageContentName":post.imageContentName!, "createdDate":post.createdDate!, "id":post.id!]
    dismissViewControllerAnimated(true) {
        FirebaseService().savePost(postArray)
    }
}

Firebase database scheme:

  • Posts "randomly generated id"

    • name: "John Doe"

    • id: same randomly generated id as above

    • postText: "Blah Blah"

Again, all I am getting for id is an empty string when I run the postPost() function.



Answers

I believe I misread your code. My bad. Here's my new answer.

I think you want to set

post["id"] = postRef.key

Before you call

postRef.setValue(post)


The problem is savePost is what sets the id value in FirebaseService. Your attempting to assign the value of the id before the savePost function gets called and assigns keys value to id.

Try having your savePost function take a Post object as the argument, then set up its format to uploaded to Firebase in that function. It will allow you to set the Post.id from the reference you create in that function.

func postPost() {
//remove line assigned post.id
post.textContent = textView.text
post.name = "Michael Williams"
post.profileImageName = "cool"
post.imageContentName = "coffee"
post.createdDate = convertCurrentTimeToString()
//Remove postArray
dismissViewControllerAnimated(true) {
    FirebaseService().savePost(post)
}}

Assign post.id in savePost function:

func savePost(post: Post) {
    let postRefKey = ref.child("posts").childByAutoId()
    //Get key
    let postRef = ref.child("posts")
    //Generate base reference to all posts
    post.id = postRefKey
    let postDict = ["postId": post.id, "postName" : post.name] //etc
    //save post to postsReference using the generate key and then set that keys value to Post Object
    postRef.child(postRefKey).setValue(postDict)


}

Another way you could do it is with a closure using a completion block.

func savePost(post: Post, completion: String -
By : JustinM


This video can help you solving your question :)
By: admin