-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathParseStorage.swift
58 lines (50 loc) · 1.63 KB
/
ParseStorage.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
//
// ParseStorage.swift
//
//
// Created by Pranjal Satija on 7/19/20.
//
// MARK: ParseStorage
struct ParseStorage {
public static var shared = ParseStorage()
private var backingStore: ParsePrimitiveStorable?
mutating func use(_ store: ParsePrimitiveStorable) {
self.backingStore = store
}
private mutating func requireBackingStore() {
guard backingStore != nil else {
print("""
You cannot use ParseStorage without a backing store.
An in-memory store is being used as a fallback.
""")
return
}
}
enum Keys {
static let currentUser = "_currentUser"
static let currentInstallation = "_currentInstallation"
static let currentConfig = "_currentConfig"
static let defaultACL = "_defaultACL"
static let currentVersion = "_currentVersion"
static let currentAccessGroup = "_currentAccessGroup"
}
}
// MARK: ParseKeyValueStore
extension ParseStorage: ParsePrimitiveStorable {
public mutating func delete(valueFor key: String) throws {
requireBackingStore()
try backingStore?.delete(valueFor: key)
}
public mutating func deleteAll() throws {
requireBackingStore()
try backingStore?.deleteAll()
}
public mutating func get<T>(valueFor key: String) throws -> T? where T: Decodable {
requireBackingStore()
return try backingStore?.get(valueFor: key)
}
public mutating func set<T>(_ object: T, for key: String) throws where T: Encodable {
requireBackingStore()
try backingStore?.set(object, for: key)
}
}