Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A remote class of type Items
// MARK: Frameworks
import Foundation
import FirebaseDatabase
// MARK: ItemsRemote
class ItemsRemote {
// MARK: Constants
static let itemsReference = Database.database().reference().child("items")
// MARK: POST Methods
static func postItem(item: Item) {
itemsReference.childByAutoId().setValue(item.toAnyObject())
}
// MARK: GET Methods
static func getAllItems(completion: @escaping(_ items: [Item]) -> Void) {
itemsReference.observe(.value) { snapshot in
var items: [Item] = []
for item in snapshot.children {
guard let item = item as? DataSnapshot,
let currentItem = Item(snapshot: item) else {
continue
}
items.append(currentItem)
}
completion(items)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment