Skip to content

Instantly share code, notes, and snippets.

@rtking1993
Created March 30, 2018 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtking1993/7eb1c2c668bc01d80beddceb923690e2 to your computer and use it in GitHub Desktop.
Save rtking1993/7eb1c2c668bc01d80beddceb923690e2 to your computer and use it in GitHub Desktop.
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