繁体   English   中英

在Firebase数据服务类中设置一个childByAutoId值,以在另一个类中检索

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

希望我能比我的头衔更清楚。 我创建了一个FirebaseService类。 在其中,我将创建用于保存和检索数据的所有方法。 我不想在任何其他类中都做一个Firebase引用。 我只想引用此类即可。 如果无法做到这一点,请告诉我,所以我不会继续进行下去。 下面是我的FirebaseService类...

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)

    }



}

我希望能够从FirebaseService类中检索id ,并在保存帖子时让它实际使用childByAutoId的值(该帖子在Firebase中随机生成的ID),以便可以将其另存为名为“ id”的值。 但是,现在,当我保存它时,我得到了一个针对整个帖子生成的预期随机ID,但是我的“ id”键中没有该值。

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数据库方案:

  • 帖子“随机生成的ID”

    • 名称:“约翰·杜伊”

    • id:与上面相同的随机生成的id

    • postText:“ Blah Blah”

同样,当我运行postPost()函数时,我得到的id都是空字符串。

问题是savePost是在FirebaseService中设置id值的原因。 您尝试在调用savePost函数之前分配ID值并将键值分配给ID。

尝试让savePost函数将Post对象作为参数,然后将其格式设置为在该函数中上传到Firebase。 它将允许您从在该函数中创建的引用中设置Post.id。

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)
}}

在savePost函数中分配post.id:

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)


}

您可以执行的另一种方法是使用完成块进行闭合。

func savePost(post: Post, completion: String -> Void) {
//savePostFunctionHere
let key = postRefKey
completion(key)
}

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, completion: { postId in
post.id = postId
})
}}

如果您不熟悉闭包,请在savePost函数中将refKey或Postkey分配给完成参数,然后在调用该函数并完成后,将其取回ID,并可以执行任何操作在这种情况下,将其分配给post.id。

如果您想保留其他所有内容,则此方法会更好,因为它仍然可以工作,而无需进行其他更改。

我相信我误读了您的代码。 我的错。 这是我的新答案。

我想你要设定

post["id"] = postRef.key

致电之前

postRef.setValue(post)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM