@@ -212,89 +212,43 @@ extension PersistentContainer {
212
212
context. performAndWait {
213
213
userDetail = ManagedUserDetail ( context: context)
214
214
if previewUserDetail. inherits == true , let latestUserDetail = state. userDetail {
215
- userDetail. blockingUserIDs = latestUserDetail. blockingUserIDs
216
- userDetail. followerUserIDs = latestUserDetail. followerUserIDs
217
- userDetail. followerUsersCount = latestUserDetail. followerUsersCount
218
- userDetail. followingUserIDs = latestUserDetail. followingUserIDs
219
- userDetail. followingUsersCount = latestUserDetail. followingUsersCount
220
- userDetail. isProtected = latestUserDetail. isProtected
221
- userDetail. isVerified = latestUserDetail. isVerified
222
- userDetail. listedCount = latestUserDetail. listedCount
223
- userDetail. location = latestUserDetail. location
224
- userDetail. mutingUserIDs = latestUserDetail. mutingUserIDs
225
- userDetail. name = latestUserDetail. name
226
- userDetail. profileHeaderImageURL = latestUserDetail. profileHeaderImageURL
227
- userDetail. profileImageURL = latestUserDetail. profileImageURL
228
- userDetail. tweetsCount = latestUserDetail. tweetsCount
229
- userDetail. url = latestUserDetail. url
230
- userDetail. userAttributedDescription = latestUserDetail. userAttributedDescription
231
- userDetail. userCreationDate = latestUserDetail. userCreationDate
232
- userDetail. userID = latestUserDetail. userID
233
- userDetail. username = latestUserDetail. username
215
+ let attributeNames = ManagedUserDetail . entity ( ) . attributesByName. keys
216
+ userDetail. setValuesForKeys ( . init( uniqueKeysWithValues: attributeNames. map ( { ( $0, latestUserDetail. value ( forKey: $0) as Any ) } ) ) )
217
+ userDetail. creationDate = nil
234
218
}
235
- if let blockingUserIDs = previewUserDetail. blockingUserIDs {
236
- userDetail. blockingUserIDs = blockingUserIDs
237
- }
238
- if let creationDate = previewUserDetail. creationDate {
239
- userDetail. creationDate = creationDate
240
- }
241
- else {
242
- userDetail. creationDate = . now
243
- }
244
- if let followerUserIDs = previewUserDetail. followerUserIDs {
245
- userDetail. followerUserIDs = followerUserIDs
246
- }
247
- if let followerUsersCount = previewUserDetail. followerUsersCount {
248
- userDetail. followerUsersCount = . init( followerUsersCount)
249
- }
250
- if let followingUserIDs = previewUserDetail. followingUserIDs {
251
- userDetail. followingUserIDs = followingUserIDs
252
- }
253
- if let followingUsersCount = previewUserDetail. followingUsersCount {
254
- userDetail. followingUsersCount = . init( followingUsersCount)
255
- }
256
- if let isProtected = previewUserDetail. isProtected {
257
- userDetail. isProtected = isProtected
258
- }
259
- if let isVerified = previewUserDetail. isVerified {
260
- userDetail. isVerified = isVerified
261
- }
262
- if let listedCount = previewUserDetail. listedCount {
263
- userDetail. listedCount = . init( listedCount)
264
- }
265
- if let location = previewUserDetail. location {
266
- userDetail. location = location
267
- }
268
- if let mutingUserIDs = previewUserDetail. mutingUserIDs {
269
- userDetail. mutingUserIDs = mutingUserIDs
270
- }
271
- if let name = previewUserDetail. name {
272
- userDetail. name = name
273
- }
274
- if let profileHeaderImageURL = previewUserDetail. profileHeaderImageURL {
275
- userDetail. profileHeaderImageURL = profileHeaderImageURL
276
- }
277
- if let profileImageURL = previewUserDetail. profileImageURL {
278
- userDetail. profileImageURL = profileImageURL
279
- }
280
- if let tweetsCount = previewUserDetail. tweetsCount {
281
- userDetail. tweetsCount = . init( tweetsCount)
282
- }
283
- if let url = previewUserDetail. url {
284
- userDetail. url = url
285
- }
286
- if let userAttributedDescription = previewUserDetail. userAttributedDescription {
287
- userDetail. userAttributedDescription = . init( userAttributedDescription)
288
- }
289
- if let userCreationDate = previewUserDetail. userCreationDate {
290
- userDetail. userCreationDate = userCreationDate
291
- }
292
- if let userID = previewUserDetail. userID {
293
- userDetail. userID = userID
219
+ func assign< T> ( _ value: T ? , to keyPath: ReferenceWritableKeyPath < ManagedUserDetail , T > ) {
220
+ if let value = value {
221
+ userDetail [ keyPath: keyPath] = value
222
+ }
294
223
}
295
- if let username = previewUserDetail. username {
296
- userDetail. username = username
224
+ func assign< T> ( _ value: T ? , to keyPath: ReferenceWritableKeyPath < ManagedUserDetail , T ? > , default: T ? = nil ) {
225
+ if let value = value {
226
+ userDetail [ keyPath: keyPath] = value
227
+ }
228
+ else if userDetail![ keyPath: keyPath] == nil , let `default` = `default` {
229
+ userDetail [ keyPath: keyPath] = `default`
230
+ }
297
231
}
232
+ assign ( previewUserDetail. blockingUserIDs, to: \. blockingUserIDs)
233
+ assign ( previewUserDetail. creationDate, to: \. creationDate, default: . now)
234
+ assign ( previewUserDetail. followerUserIDs, to: \. followerUserIDs)
235
+ assign ( previewUserDetail. followerUsersCount. flatMap ( { . init( $0) } ) , to: \. followerUsersCount)
236
+ assign ( previewUserDetail. followingUserIDs, to: \. followingUserIDs)
237
+ assign ( previewUserDetail. followingUsersCount. flatMap ( { . init( $0) } ) , to: \. followingUsersCount)
238
+ assign ( previewUserDetail. isProtected, to: \. isProtected)
239
+ assign ( previewUserDetail. isVerified, to: \. isVerified)
240
+ assign ( previewUserDetail. listedCount. flatMap ( { . init( $0) } ) , to: \. listedCount)
241
+ assign ( previewUserDetail. location, to: \. location)
242
+ assign ( previewUserDetail. mutingUserIDs, to: \. mutingUserIDs)
243
+ assign ( previewUserDetail. name, to: \. name)
244
+ assign ( previewUserDetail. profileHeaderImageURL, to: \. profileHeaderImageURL)
245
+ assign ( previewUserDetail. profileImageURL, to: \. profileImageURL)
246
+ assign ( previewUserDetail. tweetsCount. flatMap ( { . init( $0) } ) , to: \. tweetsCount)
247
+ assign ( previewUserDetail. url, to: \. url)
248
+ assign ( previewUserDetail. userAttributedDescription. flatMap ( { . init( $0) } ) , to: \. userAttributedDescription)
249
+ assign ( previewUserDetail. userCreationDate, to: \. userCreationDate)
250
+ assign ( previewUserDetail. userID, to: \. userID)
251
+ assign ( previewUserDetail. username, to: \. username)
298
252
if save {
299
253
do {
300
254
try context. save ( )
0 commit comments