Slide 38
Slide 38 text
final class FirebaseDatabaseManager {
private var reference: FIRDatabaseReference = FIRDatabase.database().reference().child("articles")
func save(_ article: Article, completion: CompletionHandler?) {
reference.child(article.id).setValue(article.encoded) { _, _ in
completion?()
}
}
func load(_ completionHandler: @escaping ([Article]) -> ()) {
reference.observeSingleEvent(of: .value) { snapshot -> Void in
completionHandler(Article.build(snapshot.value))
}
}
func added(_ completionHandler: @escaping (Article?) -> ()) {
let handler: (FIRDataSnapshot) -> Void = { snapshot in
completionHandler(Article.decodeSingle(snapshot))
}
reference.observe(.childAdded, with: handler)
}
'JSFCBTF4%,Λ͍ɺ3FBMUJNF%BUBCBTFͷมߋΛࢹ