|
| 1 | +// |
| 2 | +// CKError-Extras.swift |
| 3 | +// SQLiteDB |
| 4 | +// |
| 5 | +// Created by Fahim Farook on 23-10-2020. |
| 6 | +// Copyright © 2020 RookSoft Ltd. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import CloudKit |
| 10 | + |
| 11 | +extension CKError { |
| 12 | + public func isRecordNotFound() -> Bool { |
| 13 | + isZoneNotFound() || isUnknownItem() |
| 14 | + } |
| 15 | + |
| 16 | + public func isZoneNotFound() -> Bool { |
| 17 | + isSpecificErrorCode(code: .zoneNotFound) |
| 18 | + } |
| 19 | + |
| 20 | + public func isUnknownItem() -> Bool { |
| 21 | + isSpecificErrorCode(code: .unknownItem) |
| 22 | + } |
| 23 | + |
| 24 | + public func isConflict() -> Bool { |
| 25 | + isSpecificErrorCode(code: .serverRecordChanged) |
| 26 | + } |
| 27 | + |
| 28 | + public func isSpecificErrorCode(code: CKError.Code) -> Bool { |
| 29 | + var match = false |
| 30 | + if self.code == code { |
| 31 | + match = true |
| 32 | + } else if self.code == .partialFailure { |
| 33 | + // This is a multiple-issue error. Check the underlying array |
| 34 | + // of errors to see if it contains a match for the error in question. |
| 35 | + guard let errors = partialErrorsByItemID else { |
| 36 | + return false |
| 37 | + } |
| 38 | + for (_, error) in errors { |
| 39 | + if let cke = error as? CKError { |
| 40 | + if cke.code == code { |
| 41 | + match = true |
| 42 | + break |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + return match |
| 48 | + } |
| 49 | + |
| 50 | + // ServerRecordChanged errors contain the CKRecord information |
| 51 | + // for the change that failed, allowing the client to decide |
| 52 | + // upon the best course of action in performing a merge. |
| 53 | + public func getMergeRecords() -> (CKRecord?, CKRecord?) { |
| 54 | + if code == .serverRecordChanged { |
| 55 | + // This is the direct case of a simple serverRecordChanged Error. |
| 56 | + return (clientRecord, serverRecord) |
| 57 | + } |
| 58 | + guard code == .partialFailure else { |
| 59 | + return (nil, nil) |
| 60 | + } |
| 61 | + guard let errors = partialErrorsByItemID else { |
| 62 | + return (nil, nil) |
| 63 | + } |
| 64 | + for (_, error) in errors { |
| 65 | + if let cke = error as? CKError { |
| 66 | + if cke.code == .serverRecordChanged { |
| 67 | + // This is the case of a serverRecordChanged Error |
| 68 | + // contained within a multi-error PartialFailure Error. |
| 69 | + return cke.getMergeRecords() |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + return (nil, nil) |
| 74 | + } |
| 75 | +} |
0 commit comments