Skip to content

Instantly share code, notes, and snippets.

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

  • Save phucnm/245adac78d98dd38d51efd19439b1c66 to your computer and use it in GitHub Desktop.

Select an option

Save phucnm/245adac78d98dd38d51efd19439b1c66 to your computer and use it in GitHub Desktop.
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