Last active
August 11, 2017 09:48
-
-
Save phucnm/6be79b351abbbf8b5bfe42b351c8557c to your computer and use it in GitHub Desktop.
First-Post-Model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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