-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceFirebaseRealTimeDatabaseProtocol.swift
479 lines (456 loc) · 19.8 KB
/
ServiceFirebaseRealTimeDatabaseProtocol.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//
// Project: FirebaseRealTimeDatabase
// File: ServiceFirebaseRealTimeDatabaseProtocol.swift
// Copyright © 2021 Oguz Yuksel. All rights reserved.
//
// Created by Oguz Yuksel([email protected]) on 18.09.2021.
//
import Foundation
import FirebaseDatabase
import CodableFirebase
import Algorithms
// MARK: - Protocol
protocol ServiceFirebaseRealTimeDatabaseProtocol: ObservableObject {
typealias ObservationType = Database.ModelObserver.ObservationType
typealias OrderType = Database.ModelObserver.QueryOrder
typealias FilterType = Database.ModelObserver.QueryFilter
typealias ObserverModel = Database.ModelObserver
typealias ServiceError = Database.ServiceError
var database: Database { get }
var dbRef: DatabaseReference { get }
var refObservers: [ObserverModel : DatabaseHandle] { get set }
func create<T: Codable>(childPath: String, value: T) async throws -> Void
func create<T: Codable>(childPaths: [String], value: T) async throws -> Void
func update<T: Codable>(childPath: String, value: T) async throws -> Void
func update<T: Codable>(childPaths: [String], value: T) async throws -> Void
func write<T: Codable>(childPath: String, value: T) async throws -> Void
func write<T: Codable>(childPaths: [String], value: T) async throws -> Void
func remove(childPath: String) async throws -> Void
func addObserver<T: Codable>(model: ObserverModel, completion: @escaping (_ jsonValue: Result<T?,Error>) -> Void)
func getData<T: Codable>(childPath: String) async throws -> T?
func removeObserver(model: ObserverModel) throws -> Void
func removeAllObservers(childPath: String) throws -> Void
func deInit()
}
// TODO: Add Transactions
// TODO: Consider Opaque Types instead of Generics
// TODO: Fix Documentation Typos!
extension ServiceFirebaseRealTimeDatabaseProtocol {
// MARK: Properties
var dbRef: DatabaseReference { database.reference() }
// MARK: Functions
/// `Create` a value for given path. In order to `Create`, value of given path key must be empty.
/// - Parameters:
/// - childPath: Path of `value`.
/// - value: `value` of `childPath`.
func create<T: Codable>(childPath: String = "/", value: T) async throws -> Void {
if try await (getData(childPath: childPath) as T?) == nil {
try await write(childPath: childPath, value: value) // Path is empty, you can write.
}
else {
throw ServiceError.pathIsFull
}
}
/// `Create` a value for given path. In order to `Create`, value of given path key must be empty.
/// - Parameters:
/// - childPaths: Path of `value`.
/// - value: `value` of `childPath`.
func create<T: Codable>(childPaths: [String], value: T) async throws -> Void {
for path in childPaths {
if try await (getData(childPath: path) as T?) != nil {
throw ServiceError.pathIsFull
}
}
try await write(childPaths: childPaths, value: value) // Paths are empty, you can write.
}
/// `Update` value of given path. In order to `Update`, given path key must have a value.
/// - Parameters:
/// - childPath: Path of `value`.
/// - value: `value` of `childPath`.
func update<T: Codable>(childPath: String = "/", value: T) async throws -> Void {
if try await (getData(childPath: childPath) as T?) != nil {
try await write(childPath: childPath, value: value) // Path is not empty, you can write.
}
else {
throw ServiceError.pathIsEmpty
}
}
/// `Update` value of given path. In order to `Update`, given path key must have a value.
/// - Parameters:
/// - childPaths: Path of `value`.
/// - value: `value` of `childPath`.
func update<T: Codable>(childPaths: [String], value: T) async throws -> Void {
for path in childPaths {
if try await (getData(childPath: path) as T?) == nil {
throw ServiceError.pathIsEmpty
}
}
try await write(childPaths: childPaths, value: value) // Paths are not empty, you can write.
}
/// `Write` value of given path. `Write` is a forced method so that there is value or not, it will write!
/// - Parameters:
/// - childPath: Path of `value`.
/// - value: `value` of `childPath`.
func write<T: Codable>(childPath: String = "/", value: T) async throws -> Void {
do {
try await dbRef.child(childPath).setValue(value)
} catch {
throw ServiceError.writingFailed
}
}
/// `Write` value of given path. `Write` is a forced method so that there is value or not, it will write!
/// - Parameters:
/// - childPaths: Path of `value`.
/// - value: `value` of `childPath`.
func write<T: Codable>(childPaths: [String], value: T) async throws -> Void {
// Simultaneous updates made this way are atomic: either all updates succeed or all updates fail.
let composedPaths = composePaths(childPaths)
print("ServiceDatabaseProtocol.write(): writes following paths \(composedPaths)")
let childUpdates = Dictionary(uniqueKeysWithValues: composedPaths.map { ($0, value) })
do {
try await dbRef.updateChildValues(childUpdates)
} catch {
throw ServiceError.writingFailed
}
}
/// `Remove` a value.
/// - Parameter childPath: Path of `value`.
func remove(childPath: String = "/") async throws -> Void {
do {
try await dbRef.child(childPath).removeValue()
} catch {
throw ServiceError.removingFailed
}
}
/// Observe a database `path` according to observation `type`.
///
/// # Database Structure
/// /users
/// /17865
/// /name: "Oguz Yuksel"
/// /email: "[email protected]"
/// /gender: "M"
/// /age: 27
/// /25789
/// /name: "Clark Kent"
/// /email: "[email protected]"
/// /gender: "M"
/// /age: 22
/// /29874
/// /name: "Nancy Brown"
/// /email: "[email protected]"
/// /gender: "F"
/// /age: 52
/// /29894
/// /name: "Ela Twin"
/// /email: "[email protected]"
/// /gender: "F"
/// /age: 25
/// /posts
/// /92
/// /title: "First Post"
/// /context: "Something happened!"
/// /493
/// /title: "Third Post"
/// /context: "Something happened!"
/// /123
/// /title: "Second Post"
/// /context: "Something again happened!"
///
/// # Query samples
/// - queryOrdered(byChild: String) & queryEqual(toValue: Any)
///
/// ref.child(/users).queryOrdered(byChild: "gender").queryEqual(toValue: "F", childKey: "29894")<.observe/.getData/.observeSingleEvent>
/// Returns a **disordered JSON** as below, **.sort()** returned value before using!
///
/// // {
/// // "29874" = {
/// // name: "Nancy Brown";
/// // email: "[email protected]";
/// // gender: "F";
/// // age: 52;
/// // };
/// // "29894" = {
/// // name: "Ela Twin";
/// // email: "[email protected]";
/// // gender: "F";
/// // age: 25;
/// // };
/// // };
///
/// - queryOrdered(byChild: String) & queryEqual(toValue: Any, childKey: String)
///
/// ref.child(/users).queryOrdered(byChild: "gender").queryEqual(toValue: "F", childKey: "29894")<.observe/.getData/.observeSingleEvent>
/// Returns a **disordered JSON** as below, **.sort()** returned value before using!
///
/// // {
/// // "29894" = {
/// // name: "Ela Twin";
/// // email: "[email protected]";
/// // gender: "F";
/// // age: 25;
/// // };
/// // };
///
/// - queryOrdered(byChild: String) & queryEnding(beforeValue: Any)
///
/// queryEnding(beforeValue: Any, childKey: String)
///
/// Inferences can be made for this version by looking at the previous examples.
///
/// ref.child(/users).queryOrdered(byChild: "age").queryEnding(beforeValue:52)<.observe/.getData/.observeSingleEvent>
/// Returns a **disordered JSON** as below, **.sort()** returned value before using!
///
/// // {
/// // "17865" = {
/// // name: "Oguz Yuksel";
/// // email: "[email protected]";
/// // gender: "M";
/// // age: 27;
/// // };
/// // "25789" = {
/// // name: "Clark Kent";
/// // email: "[email protected]";
/// // gender: "M";
/// // age: 22;
/// // };
/// // "29894" = {
/// // name: "Ela Twin";
/// // email: "[email protected]";
/// // gender: "F";
/// // age: 25;
/// // };
/// // };
///
/// - queryOrdered(byChild: String) & queryEnding(atValue: Any)
///
/// queryEnding(atValue: Any, childKey: String)
///
/// Inferences can be made for this version by looking at the previous examples.
///
/// ref.child(/users).queryOrdered(byChild: "age").queryEnding(atValue:52)<.observe/.getData/.observeSingleEvent>
/// Returns a **disordered JSON** as below, **.sort()** returned value before using!
///
/// // {
/// // "17865" = {
/// // name: "Oguz Yuksel";
/// // email: "[email protected]";
/// // gender: "M";
/// // age: 27;
/// // };
/// // "25789" = {
/// // name: "Clark Kent";
/// // email: "[email protected]";
/// // gender: "M";
/// // age: 22;
/// // };
/// // "29874" = {
/// // name: "Nancy Brown";
/// // email: "[email protected]";
/// // gender: "F";
/// // age: 52;
/// // };
/// // "29894" = {
/// // name: "Ela Twin";
/// // email: "[email protected]";
/// // gender: "F";
/// // age: 25;
/// // };
/// // };
///
/// - queryOrdered(byChild: String) & queryLimited(toFirst: Int)
///
/// queryLimited(toLast: Int)
///
/// Inferences can be made for this version by looking at the previous examples.
///
/// ref.child(/users).queryOrdered(byChild: "age").queryLimited(toFirst:2)<.observe/.getData/.observeSingleEvent>
/// Returns a **disordered JSON** as below, **.sort()** returned value before using!
///
/// // {
/// // "25789" = {
/// // name: "Clark Kent";
/// // email: "[email protected]";
/// // gender: "M";
/// // age: 22;
/// // };
/// // "29894" = {
/// // name: "Ela Twin";
/// // email: "[email protected]";
/// // gender: "F";
/// // age: 25;
/// // };
/// // };
///
/// - queryOrderedByKey() & queryLimited(toFirst: Int)
///
/// queryOrderedByKey().<otherQueryTypes>
///
/// queryOrderedByValue().<otherQueryTypes>
///
/// Inferences can be made for these versions by looking at the previous examples.
///
/// ref.child(/posts).queryOrderedByKey().queryLimited(toFirst:2)<.observe/.getData/.observeSingleEvent>
/// Returns a **disordered JSON** as below, **.sort()** returned value before using!
///
/// // {
/// // "92" = {
/// // title: "First Post";
/// // context: "Something happened!";
/// // };
/// // "123" = {
/// // title: "Second Post";
/// // context: "Something again happened!";
/// // };
/// // };
///
/// - Parameters:
/// - model: Observer Model.
/// - completion: Completion will run each time after getting a new value from observer. You may use that update something through ViewModel in UI with new value.
/// - jsonValue: JSON model of observed path.
func addObserver<T: Codable>(model: ObserverModel, completion: @escaping (_ jsonValue: Result<T?,Error>) -> Void) {
guard refObservers[model] == nil else { completion(.failure(ServiceError.observerAlreadyExists)); return } // Check if observer already added.
var dbQuery = dbRef.child(model.childPath) as DatabaseQuery
switch model.order {
case .queryOrderedByKey:
dbQuery = dbQuery.queryOrderedByKey()
case .queryOrderedByValue:
dbQuery = dbQuery.queryOrderedByValue()
case .queryOrdered(let byChild):
dbQuery = dbQuery.queryOrdered(byChild: byChild)
default:
break
}
switch model.filter {
case .queryEqualToValue(let toValue, childKey: let childKey):
dbQuery = dbQuery.queryEqual(toValue: toValue, childKey: childKey)
case .queryEndingAtValue(let atValue, childKey: let childKey):
dbQuery = dbQuery.queryEnding(atValue: atValue, childKey: childKey)
case .queryEndingBeforeValue(let beforeValue, childKey: let childKey):
dbQuery = dbQuery.queryEnding(beforeValue: beforeValue, childKey: childKey)
case .queryStartingAtValue(let atValue, childKey: let childKey):
dbQuery = dbQuery.queryStarting(atValue: atValue, childKey: childKey)
case .queryStartingAfterValue(let afterValue, childKey: let childKey):
dbQuery = dbQuery.queryStarting(afterValue: afterValue, childKey: childKey)
case .queryLimitedToFirst(let toFirst):
dbQuery = dbQuery.queryLimited(toFirst: toFirst)
case .queryLimitedToLast(let toLast):
dbQuery = dbQuery.queryLimited(toLast: toLast)
default:
break
}
// Completion will run each time after getting a new value from observer.
switch model.observationType {
case .observeOnce(event: let event):
dbQuery.observeSingleEvent(of: event) { jsonSnap in
completionFunc(jsonSnap: jsonSnap)
}
case .observe(event: let event):
refObservers[model] = dbQuery.observe(event) { jsonSnap in
completionFunc(jsonSnap: jsonSnap)
}
}
// Helper function
func completionFunc(jsonSnap: DataSnapshot) {
do {
let value: T? = try snapDecoder(jsonSnap)
completion(.success(value))
} catch let error {
completion(.failure(error))
}
}
}
/// Get current value from a specific `Database Path`
///
/// - Parameter childPath: Path of value.
/// - Returns: Optional type safe value.
func getData<T: Codable>(childPath: String = "/") async throws -> T? {
do {
let snapshot = try await dbRef.child(childPath).getData()
let model: T? = try snapDecoder(snapshot)
#if DEBUG
print("ServiceDatabaseProtocol.getData(): \(String(describing: model))")
#endif
return model
} catch {
throw ServiceError.gettingDataFailed
}
}
/// Remove a observer.
///
/// # Info:
/// - Observer will be also removed from `refObservers` dictionary in case of active Observers list need.
///
/// - Parameter model: Observer Model.
func removeObserver(model: ObserverModel) throws -> Void {
guard let observerID = refObservers[model] else { throw ServiceError.observerDoesntExist }
dbRef.child(model.childPath).removeObserver(withHandle: observerID)
refObservers.removeValue(forKey: model)
#if DEBUG
print("ServiceDatabaseProtocol.removeObserver(): Removed observer: \(model)")
#endif
}
/// Removes all observers at the current reference, but does not remove any observers at child references.
/// `removeAllObservers()` must be called again for each child reference where a listener was established to remove the observers.
///
/// # Info:
/// - Observers will be also removed from `refObservers` dictionary.
func removeAllObservers(childPath: String = "/") throws -> Void {
var removedKeys = [String]()
refObservers.forEach { (model, _) in
if model.childPath == childPath { refObservers.removeValue(forKey: model); removedKeys.append(model.key) }
}
guard removedKeys.count > 0 else { throw ServiceError.observerDoesntExist }
dbRef.child(childPath).removeAllObservers()
#if DEBUG
print("ServiceDatabaseProtocol.removeAllObservers(): Removed observers[\(removedKeys.count)]: \(removedKeys)")
#endif
}
/// It is a must to put this function in the deinit of Class.
func deInit() {
// Clean all observers before deinitialization!
refObservers.keys.uniqued { model in model.childPath }.forEach { uniqueModel in
dbRef.child(uniqueModel.childPath).removeAllObservers()
}
}
// MARK: Helper Functions
/// Turn unsafe child paths into safer by avoiding overwriting considering path hierarchy.
///
/// var str = ["/", "first", "first/second", "second", "third/four", "third/four/five", "third/six/seven"]
/// composePaths(str)
/// // ["/", "first", "second", "third/four", "third/six/seven"]
/// - Parameter str: Unsafe path array may contain duplicated paths.
/// - Returns: Safe path array free from overwriting for ref.updateChildValues().
private func composePaths(_ str: [String]) -> [String] {
var highestPaths = [String]()
var higherPath: String?
let sortedPaths = str.sorted()
for path in sortedPaths {
guard let validHigherPath = higherPath, path.hasPrefix(validHigherPath) else {
highestPaths.append(path)
higherPath = path
continue
}
}
#if DEBUG
print("ServiceDatabaseProtocol.composePaths(): Composed from: \(str), to: \(highestPaths)")
#endif
return highestPaths
}
/// Decode DataSnapshot JSON value into its Swift Model.
///
/// - Parameter snap: DataShapshot from observed or getData path.
/// - Returns: Optional type safe value.
private func snapDecoder<T: Codable>(_ snap: DataSnapshot) throws -> T? {
guard let json = snap.value else { return nil }
do {
let model = try FirebaseDecoder().decode(T.self, from: json)
#if DEBUG
print("ServiceDatabaseProtocol.snapDecoder(): Model: \(model)")
#endif
return model
} catch {
throw ServiceError.decodingFailed
}
}
}