-
Notifications
You must be signed in to change notification settings - Fork 150
/
CKError-Extras.swift
executable file
·75 lines (68 loc) · 1.86 KB
/
CKError-Extras.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
//
// CKError-Extras.swift
// SQLiteDB
//
// Created by Fahim Farook on 23-10-2020.
// Copyright © 2020 RookSoft Ltd. All rights reserved.
//
import CloudKit
extension CKError {
public func isRecordNotFound() -> Bool {
isZoneNotFound() || isUnknownItem()
}
public func isZoneNotFound() -> Bool {
isSpecificErrorCode(code: .zoneNotFound)
}
public func isUnknownItem() -> Bool {
isSpecificErrorCode(code: .unknownItem)
}
public func isConflict() -> Bool {
isSpecificErrorCode(code: .serverRecordChanged)
}
public func isSpecificErrorCode(code: CKError.Code) -> Bool {
var match = false
if self.code == code {
match = true
} else if self.code == .partialFailure {
// This is a multiple-issue error. Check the underlying array
// of errors to see if it contains a match for the error in question.
guard let errors = partialErrorsByItemID else {
return false
}
for (_, error) in errors {
if let cke = error as? CKError {
if cke.code == code {
match = true
break
}
}
}
}
return match
}
// ServerRecordChanged errors contain the CKRecord information
// for the change that failed, allowing the client to decide
// upon the best course of action in performing a merge.
public func getMergeRecords() -> (CKRecord?, CKRecord?) {
if code == .serverRecordChanged {
// This is the direct case of a simple serverRecordChanged Error.
return (clientRecord, serverRecord)
}
guard code == .partialFailure else {
return (nil, nil)
}
guard let errors = partialErrorsByItemID else {
return (nil, nil)
}
for (_, error) in errors {
if let cke = error as? CKError {
if cke.code == .serverRecordChanged {
// This is the case of a serverRecordChanged Error
// contained within a multi-error PartialFailure Error.
return cke.getMergeRecords()
}
}
}
return (nil, nil)
}
}