From dcb57ead069ab59efdcad755883ddea8b32e1c8e Mon Sep 17 00:00:00 2001 From: 1_am_a_geek Date: Thu, 18 May 2017 10:58:07 +0900 Subject: [PATCH] Support for Firebase 4.0 --- Podfile.lock | 46 +++++++------- SaladBar/AppDelegate.swift | 4 +- SaladBar/FileTestViewController.swift | 6 +- SaladBar/SaladaFileTestViewController.swift | 2 +- SaladBar/ViewController.swift | 2 +- Salada.podspec | 2 +- Salada/Referenceable.swift | 20 +++--- Salada/Salada+Datasource.swift | 12 ++-- Salada/Salada.swift | 70 ++++++++++----------- SaladaTests/SaladaTests.swift | 2 +- 10 files changed, 84 insertions(+), 82 deletions(-) diff --git a/Podfile.lock b/Podfile.lock index 9e6a030..656fca0 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -1,24 +1,26 @@ PODS: - - Firebase/Core (3.15.0): - - FirebaseAnalytics (= 3.7.0) - - FirebaseCore (= 3.5.2) - - Firebase/Database (3.15.0): + - Firebase/Core (4.0.0): + - FirebaseAnalytics (= 4.0.0) + - FirebaseCore (= 4.0.0) + - Firebase/Database (4.0.0): - Firebase/Core - - FirebaseDatabase (= 3.1.2) - - Firebase/Storage (3.15.0): + - FirebaseDatabase (= 4.0.0) + - Firebase/Storage (4.0.0): - Firebase/Core - - FirebaseStorage (= 1.1.0) - - FirebaseAnalytics (3.7.0): - - FirebaseCore (~> 3.5) - - FirebaseInstanceID (~> 1.0) + - FirebaseStorage (= 2.0.0) + - FirebaseAnalytics (4.0.0): + - FirebaseCore (~> 4.0) + - FirebaseInstanceID (~> 2.0) - GoogleToolboxForMac/NSData+zlib (~> 2.1) - - FirebaseCore (3.5.2): + - FirebaseCore (4.0.0): - GoogleToolboxForMac/NSData+zlib (~> 2.1) - - FirebaseDatabase (3.1.2): - - FirebaseAnalytics (~> 3.7) - - FirebaseInstanceID (1.0.9) - - FirebaseStorage (1.1.0): - - FirebaseAnalytics (~> 3.7) + - FirebaseDatabase (4.0.0): + - FirebaseAnalytics (~> 4.0) + - FirebaseInstanceID (2.0.0): + - FirebaseCore (~> 4.0) + - FirebaseStorage (2.0.0): + - FirebaseAnalytics (~> 4.0) + - FirebaseCore (~> 4.0) - GTMSessionFetcher/Core (~> 1.1) - GoogleToolboxForMac/Defines (2.1.1) - GoogleToolboxForMac/NSData+zlib (2.1.1): @@ -30,12 +32,12 @@ DEPENDENCIES: - Firebase/Storage SPEC CHECKSUMS: - Firebase: 2b1cdfba1cda8589f32904a697cc753322bff9d8 - FirebaseAnalytics: 0d1b7d81d5021155be37702a94ba1ec16d45365d - FirebaseCore: a024587e43778508700af8c6b1209f7c4516ba02 - FirebaseDatabase: 05c96d7b43a7368dc91c07791adb49683e1738d1 - FirebaseInstanceID: 2d0518b1378fe9d685ef40cbdd63d2fdc1125339 - FirebaseStorage: a5c55b23741a49a72af8f30f95b3bb5ddbeda12d + Firebase: 284eea779b73fdff309791817da7c68bff8dd572 + FirebaseAnalytics: 6f08e746f7d66f5452931bc2e822b5df9c66b64a + FirebaseCore: 85ad466044c2f013cdb167f85d426d15b128114a + FirebaseDatabase: d829b3a8c3e2ac7a16773c5df226966b0805dfc2 + FirebaseInstanceID: 9fbf536668f4d3f0880e7438456dabd1376e294b + FirebaseStorage: 8110a1ed2034c8fbfd83890d2acc9cdbbd99afec GoogleToolboxForMac: 8e329f1b599f2512c6b10676d45736bcc2cbbeb0 GTMSessionFetcher: 5c046c76a1f859bc9c187e918f18e4fc7bb57b5e diff --git a/SaladBar/AppDelegate.swift b/SaladBar/AppDelegate.swift index 96074e9..6b67808 100644 --- a/SaladBar/AppDelegate.swift +++ b/SaladBar/AppDelegate.swift @@ -15,8 +15,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { - FIRApp.configure() - FIRDatabase.database().persistenceEnabled = false + FirebaseApp.configure() + Database.database().isPersistenceEnabled = false return true } diff --git a/SaladBar/FileTestViewController.swift b/SaladBar/FileTestViewController.swift index c4905bb..0ccfffa 100644 --- a/SaladBar/FileTestViewController.swift +++ b/SaladBar/FileTestViewController.swift @@ -11,7 +11,7 @@ import Firebase class FileTestViewController: UIViewController { - var task: FIRStorageUploadTask? { + var task: StorageUploadTask? { didSet { self.pause = task?.observe(.pause, handler: { (snapshot) in print(snapshot) @@ -50,9 +50,9 @@ class FileTestViewController: UIViewController { .appendingPathExtension("jpg") try! data.write(to: tmpURL) - let ref: FIRStorageReference = FIRStorage.storage().reference().child("test") + let ref: StorageReference = Storage.storage().reference().child("test") - self.task = ref.putFile(tmpURL, metadata: nil) { (metadata, error) in + self.task = ref.putFile(from: tmpURL, metadata: nil) { (metadata, error) in if let error: Error = error as Error? { print(error) diff --git a/SaladBar/SaladaFileTestViewController.swift b/SaladBar/SaladaFileTestViewController.swift index 3012577..f768105 100644 --- a/SaladBar/SaladaFileTestViewController.swift +++ b/SaladBar/SaladaFileTestViewController.swift @@ -25,7 +25,7 @@ class SaladaFileTestViewController: UIViewController { let item: Item = Item() item.file = file item.index = 0 - let task: FIRStorageUploadTask = item.save { (ref, error) in + let task: StorageUploadTask = item.save { (ref, error) in if let error = error { print(error) return diff --git a/SaladBar/ViewController.swift b/SaladBar/ViewController.swift index ed7cac5..66ffe8c 100644 --- a/SaladBar/ViewController.swift +++ b/SaladBar/ViewController.swift @@ -73,7 +73,7 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour self.view.addSubview(tableView) } - var dbRef: FIRDatabaseReference! + var dbRef: DatabaseReference! override func viewDidLoad() { super.viewDidLoad() diff --git a/Salada.podspec b/Salada.podspec index 4d9ca3c..8a7502c 100644 --- a/Salada.podspec +++ b/Salada.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "Salada" - s.version = "1.1" + s.version = "2.0" s.summary = "Salada is OR Mapper for Firebase." s.homepage = "https://github.com/1amageek/Salada" s.license = { :type => "BSD" } diff --git a/Salada/Referenceable.swift b/Salada/Referenceable.swift index 7a5193a..009da8b 100644 --- a/Salada/Referenceable.swift +++ b/Salada/Referenceable.swift @@ -14,24 +14,24 @@ import FirebaseStorage Protocol that holds a reference Firebase */ public protocol Referenceable: NSObjectProtocol { - static var database: FIRDatabaseReference { get } - static var databaseRef: FIRDatabaseReference { get } - static var storage: FIRStorageReference { get } - static var storageRef: FIRStorageReference { get } + static var database: DatabaseReference { get } + static var databaseRef: DatabaseReference { get } + static var storage: StorageReference { get } + static var storageRef: StorageReference { get } static var _path: String { get } var id: String { get } - var snapshot: FIRDataSnapshot? { get } + var snapshot: DataSnapshot? { get } var createdAt: Date { get } var value: [AnyHashable: Any] { get } var ignore: [String] { get } - init?(snapshot: FIRDataSnapshot) + init?(snapshot: DataSnapshot) } public extension Referenceable { - static var database: FIRDatabaseReference { return FIRDatabase.database().reference() } - static var databaseRef: FIRDatabaseReference { return self.database.child(self._path) } - static var storage: FIRStorageReference { return FIRStorage.storage().reference() } - static var storageRef: FIRStorageReference { return self.storage.child(self._path) } + static var database: DatabaseReference { return Database.database().reference() } + static var databaseRef: DatabaseReference { return self.database.child(self._path) } + static var storage: StorageReference { return Storage.storage().reference() } + static var storageRef: StorageReference { return self.storage.child(self._path) } } diff --git a/Salada/Salada+Datasource.swift b/Salada/Salada+Datasource.swift index ff15261..73fb0f3 100644 --- a/Salada/Salada+Datasource.swift +++ b/Salada/Salada+Datasource.swift @@ -46,13 +46,13 @@ public class Datasource where Parent: Referenceable, Parent: Sala /// DatabaseReference - public var databaseRef: FIRDatabaseReference { return FIRDatabase.database().reference() } + public var databaseRef: DatabaseReference { return Database.database().reference() } public var count: Int { return pool.count } - fileprivate(set) var parentRef: FIRDatabaseReference + fileprivate(set) var parentRef: DatabaseReference - fileprivate(set) var reference: FIRDatabaseReference + fileprivate(set) var reference: DatabaseReference fileprivate(set) var parentKey: String @@ -74,7 +74,7 @@ public class Datasource where Parent: Referenceable, Parent: Sala } } - private var addReference: FIRDatabaseQuery? + private var addReference: DatabaseQuery? fileprivate var addedHandle: UInt? fileprivate var changedHandle: UInt? @@ -108,7 +108,7 @@ public class Datasource where Parent: Referenceable, Parent: Sala guard let strongSelf = self else { return } // add - var addReference: FIRDatabaseQuery = strongSelf.reference + var addReference: DatabaseQuery = strongSelf.reference if let fiarstKey: String = strongSelf.pool.first { addReference = addReference.queryOrderedByKey().queryStarting(atValue: fiarstKey) } @@ -194,7 +194,7 @@ public class Datasource where Parent: Referenceable, Parent: Sala return } - var reference: FIRDatabaseQuery = self.reference.queryOrderedByKey() + var reference: DatabaseQuery = self.reference.queryOrderedByKey() var limit: UInt = limit if let lastKey: String = lastKey { reference = reference.queryEnding(atValue: lastKey) diff --git a/Salada/Salada.swift b/Salada/Salada.swift index 9603fd4..3255222 100644 --- a/Salada/Salada.swift +++ b/Salada/Salada.swift @@ -168,7 +168,7 @@ open class Salada { self.localTimestamp = Date() } - convenience required public init?(snapshot: FIRDataSnapshot) { + convenience required public init?(snapshot: DataSnapshot) { self.init() _setSnapshot(snapshot) } @@ -187,9 +187,9 @@ open class Salada { return tmpID } - public var snapshot: FIRDataSnapshot? { + public var snapshot: DataSnapshot? { didSet { - if let snapshot: FIRDataSnapshot = snapshot { + if let snapshot: DataSnapshot = snapshot { self.hasObserve = true guard let snapshot: [String: Any] = snapshot.value as? [String: Any] else { return } self.serverCreatedAtTimestamp = snapshot["_createdAt"] as? Double @@ -232,7 +232,7 @@ open class Salada { } } - fileprivate func _setSnapshot(_ snapshot: FIRDataSnapshot) { + fileprivate func _setSnapshot(_ snapshot: DataSnapshot) { self.snapshot = snapshot type(of: self).databaseRef.child(self.id).keepSynced(true) } @@ -324,7 +324,7 @@ open class Salada { // MARK: - Save @discardableResult - public func save() -> [String: FIRStorageUploadTask] { + public func save() -> [String: StorageUploadTask] { return self.save(nil) } @@ -333,18 +333,18 @@ open class Salada { - parameter completion: If successful reference will return. An error will return if it fails. */ @discardableResult - public func save(_ completion: ((FIRDatabaseReference?, Error?) -> Void)?) -> [String: FIRStorageUploadTask] { + public func save(_ completion: ((DatabaseReference?, Error?) -> Void)?) -> [String: StorageUploadTask] { if self.id == self.tmpID || self.id == self._id { var value: [AnyHashable: Any] = self.value - let timestamp: AnyObject = FIRServerValue.timestamp() as AnyObject + let timestamp: AnyObject = ServerValue.timestamp() as AnyObject value["_createdAt"] = timestamp value["_updatedAt"] = timestamp - var ref: FIRDatabaseReference + var ref: DatabaseReference if let id: String = self._id { ref = type(of: self).databaseRef.child(id) } else { @@ -359,7 +359,7 @@ open class Salada { return } - ref.runTransactionBlock({ (data) -> FIRTransactionResult in + ref.runTransactionBlock({ (data) -> TransactionResult in if data.value != nil { data.value = value return .success(withValue: data) @@ -387,10 +387,10 @@ open class Salada { var timeout: Float = 20 let uploadQueue: DispatchQueue = DispatchQueue(label: "salada.upload.queue") - private func saveFiles(block: ((Error?) -> Void)?) -> [String: FIRStorageUploadTask] { + private func saveFiles(block: ((Error?) -> Void)?) -> [String: StorageUploadTask] { let group: DispatchGroup = DispatchGroup() - var uploadTasks: [String: FIRStorageUploadTask] = [:] + var uploadTasks: [String: StorageUploadTask] = [:] var hasError: Error? = nil @@ -409,7 +409,7 @@ open class Salada { if subjectType == File?.self || subjectType == File.self { if let file: File = value as? File { group.enter() - if let task: FIRStorageUploadTask = file.save(key, completion: { (meta, error) in + if let task: StorageUploadTask = file.save(key, completion: { (meta, error) in if let error: Error = error { hasError = error uploadTasks.forEach({ (_, task) in @@ -453,9 +453,9 @@ open class Salada { - parameter completion: If successful reference will return. An error will return if it fails. */ - private var transactionBlock: ((FIRDatabaseReference?, Error?) -> Void)? + private var transactionBlock: ((DatabaseReference?, Error?) -> Void)? - public func transaction(key: String, value: Any, completion: ((FIRDatabaseReference?, Error?) -> Void)?) { + public func transaction(key: String, value: Any, completion: ((DatabaseReference?, Error?) -> Void)?) { self.transactionBlock = completion self.setValue(value, forKey: key) } @@ -567,8 +567,8 @@ open class Salada { // update value & update timestamp // Value will be deleted if the nil. fileprivate func updateValue(_ keyPath: String, child: String?, value: Any?) { - let reference: FIRDatabaseReference = type(of: self).databaseRef.child(self.id) - let timestamp: AnyObject = FIRServerValue.timestamp() as AnyObject + let reference: DatabaseReference = type(of: self).databaseRef.child(self.id) + let timestamp: AnyObject = ServerValue.timestamp() as AnyObject if let value: Any = value { var path: String = keyPath @@ -592,12 +592,12 @@ open class Salada { /** A function that gets all data from DB whose name is model. */ - public class func observeSingle(_ eventType: FIRDataEventType, block: @escaping ([Object]) -> Void) { + public class func observeSingle(_ eventType: DataEventType, block: @escaping ([Object]) -> Void) { self.databaseRef.observeSingleEvent(of: eventType, with: { (snapshot) in if snapshot.exists() { var children: [Object] = [] snapshot.children.forEach({ (snapshot) in - if let snapshot: FIRDataSnapshot = snapshot as? FIRDataSnapshot { + if let snapshot: DataSnapshot = snapshot as? DataSnapshot { if let tsp: Object = self.init(snapshot: snapshot) { children.append(tsp) } @@ -613,7 +613,7 @@ open class Salada { /** A function that gets data of ID within the variable form DB selected. */ - public class func observeSingle(_ id: String, eventType: FIRDataEventType, block: @escaping (Object?) -> Void) { + public class func observeSingle(_ id: String, eventType: DataEventType, block: @escaping (Object?) -> Void) { self.databaseRef.child(id).observeSingleEvent(of: eventType, with: { (snapshot) in if snapshot.exists() { if let tsp: Object = self.init(snapshot: snapshot) { @@ -625,12 +625,12 @@ open class Salada { }) } - public class func observeSingle(child key: String, equal value: String, eventType: FIRDataEventType, block: @escaping ([Object]) -> Void) { + public class func observeSingle(child key: String, equal value: String, eventType: DataEventType, block: @escaping ([Object]) -> Void) { self.databaseRef.queryOrdered(byChild: key).queryEqual(toValue: value).observeSingleEvent(of: eventType, with: { (snapshot) in if snapshot.exists() { var children: [Object] = [] snapshot.children.forEach({ (snapshot) in - if let snapshot: FIRDataSnapshot = snapshot as? FIRDataSnapshot { + if let snapshot: DataSnapshot = snapshot as? DataSnapshot { if let tsp: Object = self.init(snapshot: snapshot) { children.append(tsp) } @@ -646,12 +646,12 @@ open class Salada { /** A function that gets all data from DB whenever DB has been changed. */ - public class func observe(_ eventType: FIRDataEventType, block: @escaping ([Object]) -> Void) -> UInt { + public class func observe(_ eventType: DataEventType, block: @escaping ([Object]) -> Void) -> UInt { return self.databaseRef.observe(eventType, with: { (snapshot) in if snapshot.exists() { var children: [Object] = [] snapshot.children.forEach({ (snapshot) in - if let snapshot: FIRDataSnapshot = snapshot as? FIRDataSnapshot { + if let snapshot: DataSnapshot = snapshot as? DataSnapshot { if let tsp: Object = self.init(snapshot: snapshot) { children.append(tsp) } @@ -667,7 +667,7 @@ open class Salada { /** A function that gets data of ID within the variable from DB whenever data of the ID has been changed. */ - public class func observe(_ id: String, eventType: FIRDataEventType, block: @escaping (Object?) -> Void) -> UInt { + public class func observe(_ id: String, eventType: DataEventType, block: @escaping (Object?) -> Void) -> UInt { return self.databaseRef.child(id).observe(eventType, with: { (snapshot) in if snapshot.exists() { if let tsp: Object = self.init(snapshot: snapshot) { @@ -739,7 +739,7 @@ extension Salada { public class File: NSObject { /// Save location - public var ref: FIRStorageReference? { + public var ref: StorageReference? { if let parent: Object = self.parent { return type(of: parent).storageRef.child(parent.id).child(self.name) } @@ -756,7 +756,7 @@ extension Salada { public var name: String /// File metadata - public var metadata: FIRStorageMetadata? + public var metadata: StorageMetadata? /// Parent to hold the location where you want to save public var parent: Object? @@ -765,10 +765,10 @@ extension Salada { public var keyPath: String? /// Firebase uploading task - public fileprivate(set) weak var uploadTask: FIRStorageUploadTask? + public fileprivate(set) weak var uploadTask: StorageUploadTask? /// Firebase downloading task - public fileprivate(set) weak var downloadTask: FIRStorageDownloadTask? + public fileprivate(set) weak var downloadTask: StorageDownloadTask? // MARK: - Initialize @@ -795,13 +795,13 @@ extension Salada { // MARK: - Save - fileprivate func save(_ keyPath: String) -> FIRStorageUploadTask? { + fileprivate func save(_ keyPath: String) -> StorageUploadTask? { return self.save(keyPath, completion: nil) } - fileprivate func save(_ keyPath: String, completion: ((FIRStorageMetadata?, Error?) -> Void)?) -> FIRStorageUploadTask? { + fileprivate func save(_ keyPath: String, completion: ((StorageMetadata?, Error?) -> Void)?) -> StorageUploadTask? { if let data: Data = self.data, let parent: Object = self.parent { - self.uploadTask = self.ref?.put(data, metadata: self.metadata) { (metadata, error) in + self.uploadTask = self.ref?.putData(data, metadata: self.metadata) { (metadata, error) in self.metadata = metadata if let error: Error = error as Error? { completion?(metadata, error) @@ -816,7 +816,7 @@ extension Salada { } return self.uploadTask } else if let url: URL = self.url, let parent: Object = self.parent { - self.uploadTask = self.ref?.putFile(url, metadata: self.metadata, completion: { (metadata, error) in + self.uploadTask = self.ref?.putFile(from: url, metadata: self.metadata, completion: { (metadata, error) in self.metadata = metadata if let error: Error = error as Error? { completion?(metadata, error) @@ -837,7 +837,7 @@ extension Salada { return nil } - public func save(completion: ((FIRStorageMetadata?, Error?) -> Void)?) -> FIRStorageUploadTask? { + public func save(completion: ((StorageMetadata?, Error?) -> Void)?) -> StorageUploadTask? { guard let _: Object = self.parent, let keyPath: String = self.keyPath else { let error: ObjectError = ObjectError(kind: .invalidFile, description: "It requires data when you save the file") completion?(nil, error) @@ -849,9 +849,9 @@ extension Salada { // MARK: - Load - public func dataWithMaxSize(_ size: Int64, completion: @escaping (Data?, Error?) -> Void) -> FIRStorageDownloadTask? { + public func dataWithMaxSize(_ size: Int64, completion: @escaping (Data?, Error?) -> Void) -> StorageDownloadTask? { self.downloadTask?.cancel() - let task: FIRStorageDownloadTask? = self.ref?.data(withMaxSize: size, completion: { (data, error) in + let task: StorageDownloadTask? = self.ref?.getData(maxSize: size, completion: { (data, error) in self.downloadTask = nil completion(data, error as Error?) }) diff --git a/SaladaTests/SaladaTests.swift b/SaladaTests/SaladaTests.swift index 91ed5b9..8c16baa 100644 --- a/SaladaTests/SaladaTests.swift +++ b/SaladaTests/SaladaTests.swift @@ -21,7 +21,7 @@ class SaladaTests: XCTestCase { } private static let appConfigure: () = { - FIRApp.configure() + FirebaseApp.configure() }()