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.
Aucun commentaire:
Enregistrer un commentaire