Created
March 30, 2018 13:47
-
-
Save rtking1993/7eb1c2c668bc01d80beddceb923690e2 to your computer and use it in GitHub Desktop.
A remote class of type Items
This file contains 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
// 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