-
Notifications
You must be signed in to change notification settings - Fork 9
/
README.md
198 lines (148 loc) · 6.29 KB
/
README.md
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
<p align="center">
<img width="200" src="https://raw.githubusercontent.com/SvenTiigi/SwiftKit/gh-pages/readMeAssets/SwiftKitLogo.png" alt="CodableCloudKit Logo">
</p>
<p align="center">
<a href="https://developer.apple.com/swift/">
<img src="https://img.shields.io/badge/Swift-5.0-orange.svg?style=flat" alt="Swift 5.0">
</a>
<a href="http://cocoapods.org/pods/CodableCloudKit">
<img src="https://img.shields.io/cocoapods/v/CodableCloudKit.svg?style=flat" alt="Version">
</a>
<a href="http://cocoapods.org/pods/CodableCloudKit">
<img src="https://img.shields.io/cocoapods/p/CodableCloudKit.svg?style=flat" alt="Platform">
</a>
</p>
# CodableCloudKit
<p align="center">
CodableCloudKit allows you to easily save and retrieve Codable objects to iCloud Database (CloudKit)
</p>
## Features
- [x] ℹ️ Add CodableCloudKit features
## Example
The example application is the best way to see `CodableCloudKit` in action. Simply open the `CodableCloudKit.xcodeproj` and run the `Example` scheme.
## Installation
### CocoaPods
CodableCloudKit is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:
```bash
pod 'CodableCloudKit'
```
### Manually
If you prefer not to use any of the aforementioned dependency managers, you can integrate CodableCloudKit into your project manually. Simply drag the `Sources` Folder into your Xcode project.
## Usage
Before you start, you have to enable [CloudKit](https://developer.apple.com/library/archive/documentation/DataManagement/Conceptual/CloudKitQuickStart/EnablingiCloudandConfiguringCloudKit/EnablingiCloudandConfiguringCloudKit.html) in your app.
When you did that, go to your dashboard and create all the record types you need to save.
In this example, we will use `User`.
Add a new Field to `User` with `value` as field name and `String` as field type.
All of your objects will be saved as a String.
After that, go in INDEXES, you have to add 2 indexes to your records.
The first one is `value` with `QUERYABLE` as index type.
The second one is `modifiedAt` with `SORTABLE` as index type.
Now, we should be good.
## Example
Let's say you have a `User` model you want to sync to CloudKit. This is what the model would look like:
```swift
class User: CodableCloud {
let username: String
}
//OR
class User: CodableCloud /* OR Codable & Cloud */ {
let username: String
enum CodingKeys: String, CodingKey {
case username
}
required init(username: String) {
self.username = username
super.init()
}
required override init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.username = try container.decode(String.self, forKey: .username)
try super.init(from: decoder)
}
}
```
`CodableCloud` is a typealias of `Codable & Cloud`.
### Save
```swift
func saveInCloud(_ database: CKDatabase = CKContainer.default().privateCloudDatabase,
_ completion: ResultCompletion<CKRecord>? = nil)
```
Save method has 2 parameters : a database with a default value (PrivateCloudDatabase) and an optional completion that returns the `CKRecord`. If the object already exists in iCloud, it will update instead of creating a new record.
```swift
// The Simplest Way
user.saveInCloud()
// With another Database. In this case, the public database
user.saveInCloud(CKContainer.default().publicCloudDatabase)
// With completion
user.saveInCloud { [weak self] (result: Result<CKRecord>) in
guard let `self` = self else { return }
switch result {
case .success(_):
print("\(user.username) saved in Cloud")
case .failure(let error):
print(error.localizedDescription)
}
}
```
### Retrieve
```swift
func retrieveFromCloud(_ database: CKDatabase = CKContainer.default().privateCloudDatabase,
completion: @escaping ResultCompletion<[Self]>)
```
Retrieve method has 2 parameters : a database with a default value (PrivateCloudDatabase) and an optional completion that returns a `[CodableCloud]`.
```swift
User.retrieveFromCloud(completion: { [weak self] (result: Result<[User]>) in
guard let `self` = self else { return }
switch result {
case .success(let users):
print("\(users.count) users retrieved from Cloud")
case .failure(let error):
print(error.localizedDescription)
}
})
```
### Remove
```swift
func removeFromCloud(_ database: CKDatabase = CKContainer.default().privateCloudDatabase,
_ completion: ResultCompletion<CKRecord.ID?>? = nil)
```
Retrieve method has 2 parameters : a database with a default value (PrivateCloudDatabase) and an optional completion that returns the `CKRecord.ID` as optional.
```swift
// The Simplest Way
user.removeFromCloud()
// With another Database. In this case, the public database
user.removeFromCloud(CKContainer.default().publicCloudDatabase)
// With completion
user.removeFromCloud { [weak self] (result: Result<CKRecord.ID?>) in
guard let `self` = self else { return }
switch result {
case .success(_):
print("\(user.username) removed from Cloud")
case .failure(let error):
print(error.localizedDescription)
}
}
```
## Contributing
Contributions are very welcome 🙌
## License
```
CodableCloudKit
Copyright (c) 2019 CodableCloudKit laurent.grondin@epitech.eu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
```