GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cloudkit-sample-privatedb/PrivateDatabase/ViewModel.swift at main · fehguy/cloudkit-sample-privatedb Expand file tree/ Copy pathViewModel.swiftMore file actions 168 lines (135 loc) · 6.6 KB / Copy pathViewModel.swiftFile metadata and controls168 lines (135 loc) · 6.6 KB 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 // // ViewModel.swift // (cloudkit-samples) private-database // import os.log import CloudKit class ViewModel: ObservableObject { // MARK: - Properties /// The CloudKit container to use. Update with your own container identifier. private let container = CKContainer(identifier: Config.containerIdentifier) /// This sample uses the private database, which requires a logged in iCloud account. private lazy var database = container.privateCloudDatabase /// This sample uses a singleton record ID, referred to by this property. /// CloudKit uses `CKRecord.ID` objects to represent record IDs. private let lastPersonRecordID: CKRecord.ID /// Publish the fetched last person to our view. @Published var lastPerson = String() // MARK: - Init init(isTesting: Bool = false) { // Use a different unique record ID if testing. lastPersonRecordID = CKRecord.ID(recordName: isTesting ? "lastPersonTest" : "lastPerson") getLastPerson() } // MARK: - API /// Saves the given name as the last person in the database. /// - Parameters: /// - name: Name to attach to the record as the last person. /// - completionHandler: An optional handler to process completion `success` or `failure`. func saveRecord(name: String, completionHandler: ((Result<Void, Error>) -> Void)? = nil) { let lastPersonRecord = CKRecord(recordType: "Person", recordID: lastPersonRecordID) lastPersonRecord["name"] = name // We'll use a CKModifyRecordsOperation instead of the convenience "save" method // on CKDatabase so that we can customize savePolicy. (For this sample, we'd like // to overwrite the server version of the record in all cases, regardless of what's // on the server. let saveOperation = CKModifyRecordsOperation(recordsToSave: [lastPersonRecord]) saveOperation.savePolicy = .allKeys // This completion block will execute once for every record saved. In this sample, // we will only ever be saving a single record, so we only expect this to get called // once. saveOperation.perRecordCompletionBlock = { record, error in os_log("Record with ID \(record.recordID.recordName) was saved.") if let error = error { self.reportError(error) } self.getLastPerson() } // This completion block will execute once when the entire CKModifyRecordsOperation // is complete. We'll use it in this sample to see errors, if any. Note that we could // have used this single modifyRecordsCompletionBlock instead of the perRecordCompletionBlock // in this sample and simply parsed the array of records passed in, of which there would // only have been one in this particular sample. In this sample, we are using both for // illustrative purposes. saveOperation.modifyRecordsCompletionBlock = { _, _, error in if let error = error { self.reportError(error) // If a completion was supplied, pass along the error here. completionHandler?(.failure(error)) } else { // If a completion was supplied, like during tests, call it back now. completionHandler?(.success(())) } } database.add(saveOperation) } /// Deletes the last person record. /// - Parameter completionHandler: An optional handler to process completion `success` or `failure`. func deleteLastPerson(completionHandler: ((Result<Void, Error>) -> Void)? = nil) { database.delete(withRecordID: lastPersonRecordID) { recordID, error in if let recordID = recordID { os_log("Record with ID \(recordID.recordName) was deleted.") } if let error = error { self.reportError(error) // If a completion was supplied, pass along the error here. completionHandler?(.failure(error)) } else { // If a completion was supplied, like during tests, call it back now. completionHandler?(.success(())) } } } /// Fetches the last person record and updates the published `lastPerson` property in the VM. /// - Parameter completionHandler: An optional handler to process completion `success` or `failure`. func getLastPerson(completionHandler: ((Result<Void, Error>) -> Void)? = nil) { // Here, we will use the convenience "fetch" method on CKDatabase, instead of // CKFetchRecordsOperation, which is more flexible but also more complex. database.fetch(withRecordID: lastPersonRecordID) { record, error in if let record = record { os_log("Record with ID \(record.recordID.recordName) was fetched.") if let name = record["name"] as? String { DispatchQueue.main.async { self.lastPerson = name } } } if let error = error { self.reportError(error) // If a completion was supplied, pass along the error here. completionHandler?(.failure(error)) } else { // If a completion was supplied, like during tests, call it back now. completionHandler?(.success(())) } } } // MARK: - Helpers private func reportError(_ error: Error) { guard let ckerror = error as? CKError else { os_log("Not a CKError: \(error.localizedDescription)") return } switch ckerror.code { case .partialFailure: // Iterate through error(s) in partial failure and report each one. let dict = ckerror.userInfo[CKPartialErrorsByItemIDKey] as? [NSObject: CKError] if let errorDictionary = dict { for (_, error) in errorDictionary { reportError(error) } } // This switch could explicitly handle as many specific errors as needed, for example: case .unknownItem: os_log("CKError: Record not found.") case .notAuthenticated: os_log("CKError: An iCloud account must be signed in on device or Simulator to write to a PrivateDB.") case .permissionFailure: os_log("CKError: An iCloud account permission failure occured.") case .networkUnavailable: os_log("CKError: The network is unavailable.") default: os_log("CKError: \(error.localizedDescription)") } } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |