Skip to content

Instantly share code, notes, and snippets.

@phucnm
Last active August 11, 2017 09:48
Show Gist options
  • Select an option

  • Save phucnm/6be79b351abbbf8b5bfe42b351c8557c to your computer and use it in GitHub Desktop.

Select an option

Save phucnm/6be79b351abbbf8b5bfe42b351c8557c to your computer and use it in GitHub Desktop.
First-Post-Model
class Post: Mappable {
var postId: String?
var title: String?
var content: String?
var author: String?
func mapping(map: Map) {
postId <- map["postId"]
title <- map["title"]
content <- map["content"]
author <- map["author"]
}
required init?(map: Map) { }
}
func getPost(postId: String, completion:@escaping ((Post?) -> Void)) {
let postRef = Database.database()
.reference().child("posts").child("postId")
postRef.observeSingleEvent(of: .value, with: { (snapshot) in
guard !(snapshot.value is NSNull),
var postDict = snapshot.value as? [String: Any]
else { return }
postDict["postId"] = snapshot.key
completion(Post(JSON: postDict))
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment