Last active
August 11, 2017 09:19
-
-
Save phucnm/245adac78d98dd38d51efd19439b1c66 to your computer and use it in GitHub Desktop.
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
| extension Mapper { | |
| func mapArray(snapshot: DataSnapshot) -> [N] { | |
| return snapshot.children.map { (child) -> N? in | |
| if let childSnap = child as? DataSnapshot { | |
| return N(snapshot: childSnap) | |
| } | |
| return nil | |
| //flatMap here is a trick | |
| //to filter out `nil` values | |
| }.flatMap { $0 } | |
| } | |
| } | |
| func getPosts(completion: @escaping (([Post]) -> Void)) { | |
| let postRef = Database.database().reference() | |
| .child("posts") | |
| postRef.observeSingleEvent(of: .value) { (snapshot) in | |
| completion(Mapper<Post>().mapArray(snapshot: snapshot)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment