@@ -32,8 +32,8 @@ internal class CKEncoderKeyedContainer<Key>: CKKeyedEncoder where Key: CodingKey
32
32
extension CKEncoderKeyedContainer {
33
33
34
34
var recordID : CKRecord . ID {
35
- let zid = zoneID ?? CKRecordZone . ID ( zoneName: CKRecordZone . ID. defaultZoneName, ownerName: CKCurrentUserDefaultName)
36
- return CKRecord . ID ( recordName: object. cloudKitIdentifier, zoneID: zid )
35
+ let normalizedZone = zoneID ?? CKRecordZone . ID ( zoneName: CKRecordZone . ID. defaultZoneName, ownerName: CKCurrentUserDefaultName)
36
+ return CKRecord . ID ( recordName: object. cloudKitIdentifier, zoneID: normalizedZone )
37
37
}
38
38
39
39
var generatedRecord : CKRecord {
@@ -106,46 +106,19 @@ extension CKEncoderKeyedContainer: KeyedEncodingContainerProtocol {
106
106
private func encodeCKRecordValue( _ value: CKRecordValue , forKey key: Key ) throws {
107
107
108
108
if let data = value as? Data {
109
- let tempStr = ProcessInfo . processInfo. globallyUniqueString
110
- let filename = " \( tempStr) _file.bin "
111
- let baseURL = URL ( fileURLWithPath: NSTemporaryDirectory ( ) )
112
- let fileURL = baseURL. appendingPathComponent ( filename, isDirectory: false )
113
- try data. write ( to: fileURL, options: . atomic)
114
- let asset = CKAsset ( fileURL: fileURL)
115
- storage [ key. stringValue] = asset
116
- return
109
+ try encodeData ( data, forKey: key)
117
110
}
118
111
119
112
if let datas = value as? [ Data ] {
120
- var assets = [ CKAsset] ( )
121
- for i in 0 ..< datas. count {
122
- let data = datas [ i]
123
- let tempStr = ProcessInfo . processInfo. globallyUniqueString
124
- let filename = " \( tempStr) _file.bin "
125
- let baseURL = URL ( fileURLWithPath: NSTemporaryDirectory ( ) )
126
- let fileURL = baseURL. appendingPathComponent ( filename, isDirectory: false )
127
- try data. write ( to: fileURL, options: . atomic)
128
- let asset = CKAsset ( fileURL: fileURL)
129
- assets. append ( asset)
130
- }
131
- storage [ key. stringValue] = assets as CKRecordValue
113
+ try encodeDataArray ( datas, forKey: key)
132
114
}
133
115
134
116
if let url = value as? URL {
135
- let asset = CKAsset ( fileURL: url)
136
- storage [ key. stringValue] = asset
137
- return
117
+ encodeURL ( url, forKey: key)
138
118
}
139
119
140
120
if let urls = value as? [ URL ] {
141
- var assets = [ CKAsset] ( )
142
- for i in 0 ..< urls. count {
143
- let url = urls [ i]
144
- let asset = CKAsset ( fileURL: url)
145
- assets. append ( asset)
146
- }
147
- storage [ key. stringValue] = assets as CKRecordValue
148
- return
121
+ encodeURLArray ( urls, forKey: key)
149
122
}
150
123
151
124
/**
@@ -154,42 +127,87 @@ extension CKEncoderKeyedContainer: KeyedEncodingContainerProtocol {
154
127
this values is "lat;long".
155
128
*/
156
129
if let locationString = value as? String ,
157
- locationString. contains ( " ; " ) {
158
-
159
- let split = locationString. split ( separator: " ; " )
160
-
161
- guard let latitude = Double ( split [ 0 ] ) ,
162
- let longitude = Double ( split [ 1 ] ) else {
163
- storage [ key. stringValue] = nil
164
- return
165
- }
166
-
167
- storage [ key. stringValue] = CLLocation ( latitude: latitude, longitude: longitude)
168
- return
130
+ locationString. contains ( Constants . locationSeparator) {
131
+ encodeLocation ( fromString: locationString, forKey: key)
169
132
}
170
133
171
134
if let locationsStrings = value as? [ String ] ,
172
135
let firstString = locationsStrings. first,
173
- firstString. contains ( " ; " ) {
174
-
175
- var locations = [ CLLocation] ( )
176
- locationsStrings. forEach {
177
- let split = $0. split ( separator: " ; " )
178
- guard let latitude = Double ( split [ 0 ] ) ,
179
- let longitude = Double ( split [ 1 ] ) else {
180
- storage [ key. stringValue] = nil
181
- return
182
- }
183
- let location = CLLocation ( latitude: latitude, longitude: longitude)
184
- locations. append ( location)
185
- }
186
-
187
- storage [ key. stringValue] = locations as CKRecordValue
136
+ firstString. contains ( Constants . locationSeparator) {
137
+ encodeLocations ( fromStrings: locationsStrings, forKey: key)
188
138
}
189
139
190
140
storage [ key. stringValue] = value
191
141
}
192
142
143
+ private func encodeData( _ value: Data , forKey key: Key ) throws {
144
+ let tempStr = ProcessInfo . processInfo. globallyUniqueString
145
+ let filename = " \( tempStr) _file.bin "
146
+ let baseURL = URL ( fileURLWithPath: NSTemporaryDirectory ( ) )
147
+ let fileURL = baseURL. appendingPathComponent ( filename, isDirectory: false )
148
+ try value. write ( to: fileURL, options: . atomic)
149
+ let asset = CKAsset ( fileURL: fileURL)
150
+ storage [ key. stringValue] = asset
151
+ }
152
+
153
+ private func encodeDataArray( _ values: [ Data ] , forKey key: Key ) throws {
154
+ var assets = [ CKAsset] ( )
155
+ for i in 0 ..< values. count {
156
+ let data = values [ i]
157
+ let tempStr = ProcessInfo . processInfo. globallyUniqueString
158
+ let filename = " \( tempStr) _file.bin "
159
+ let baseURL = URL ( fileURLWithPath: NSTemporaryDirectory ( ) )
160
+ let fileURL = baseURL. appendingPathComponent ( filename, isDirectory: false )
161
+ try data. write ( to: fileURL, options: . atomic)
162
+ let asset = CKAsset ( fileURL: fileURL)
163
+ assets. append ( asset)
164
+ }
165
+ storage [ key. stringValue] = assets as CKRecordValue
166
+ }
167
+
168
+ private func encodeURL( _ value: URL , forKey key: Key ) {
169
+ let asset = CKAsset ( fileURL: value)
170
+ storage [ key. stringValue] = asset
171
+ }
172
+
173
+ private func encodeURLArray( _ values: [ URL ] , forKey key: Key ) {
174
+ var assets = [ CKAsset] ( )
175
+ for i in 0 ..< values. count {
176
+ let url = values [ i]
177
+ let asset = CKAsset ( fileURL: url)
178
+ assets. append ( asset)
179
+ }
180
+ storage [ key. stringValue] = assets as CKRecordValue
181
+ }
182
+
183
+ private func encodeLocation( fromString value: String , forKey key: Key ) {
184
+ let split = value. split ( separator: Constants . locationSeparator)
185
+
186
+ guard let latitude = Double ( split [ 0 ] ) ,
187
+ let longitude = Double ( split [ 1 ] ) else {
188
+ storage [ key. stringValue] = nil
189
+ return
190
+ }
191
+
192
+ storage [ key. stringValue] = CLLocation ( latitude: latitude, longitude: longitude)
193
+ }
194
+
195
+ private func encodeLocations( fromStrings values: [ String ] , forKey key: Key ) {
196
+ var locations = [ CLLocation] ( )
197
+ values. forEach {
198
+ let split = $0. split ( separator: Constants . locationSeparator)
199
+ guard let latitude = Double ( split [ 0 ] ) ,
200
+ let longitude = Double ( split [ 1 ] ) else {
201
+ storage [ key. stringValue] = nil
202
+ return
203
+ }
204
+ let location = CLLocation ( latitude: latitude, longitude: longitude)
205
+ locations. append ( location)
206
+ }
207
+
208
+ storage [ key. stringValue] = locations as CKRecordValue
209
+ }
210
+
193
211
private func produceReference( for value: CKEncodable ) throws -> CKRecord . Reference {
194
212
let recordID = CKRecord . ID ( recordName: value. cloudKitIdentifier)
195
213
return CKRecord . Reference ( recordID: recordID, action: . deleteSelf)
0 commit comments