Created
August 9, 2018 06:56
-
-
Save mortenbekditlevsen/da243d7e4eb53df8a12492f0363aeae7 to your computer and use it in GitHub Desktop.
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
// | |
// FirebaseService.swift | |
// SwiftyFirebase | |
// | |
// Created by Morten Bek Ditlevsen on 29/07/2018. | |
// Copyright © 2018 Ka-ching. All rights reserved. | |
// | |
import FirebaseDatabase | |
import Foundation | |
public class FirebaseService { | |
private let rootRef: DatabaseReference | |
public init(ref: DatabaseReference) { | |
self.rootRef = ref.root | |
} | |
func observeSingleEvent<T>(of type: DataEventType, | |
at path: Path<T>, | |
with block: @escaping (DecodeResult<T>) -> Void) | |
where T: Decodable { | |
let ref = rootRef.child(path.rendered) | |
ref.observeSingleEvent(of: type, with: block) | |
} | |
func observe<T>(of type: DataEventType, | |
at path: Path<T>, | |
with block: @escaping (DecodeResult<T>) -> Void) -> UInt | |
where T: Decodable { | |
let ref = rootRef.child(path.rendered) | |
return ref.observe(eventType: type, with: block) | |
} | |
func setValue<T>(at path: Path<T>, value: T) throws where T: Encodable { | |
let ref = rootRef.child(path.rendered) | |
try ref.setValue(value) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment