From 9112bfe34c0289f7c32eed81badb51a46d98e616 Mon Sep 17 00:00:00 2001 From: Guilherme Girotto Date: Thu, 4 Feb 2021 10:40:52 -0300 Subject: [PATCH 1/3] Improves example project example --- ...oudKitCodableWatch (Notification).xcscheme | 25 ++----- .../Base.lproj/Main.storyboard | 51 ++++++++++++-- NestedCloudKitCodable/ViewController.swift | 68 ++++++++++++++----- Podfile.lock | 2 +- 4 files changed, 103 insertions(+), 43 deletions(-) diff --git a/NestedCloudKitCodable.xcodeproj/xcshareddata/xcschemes/NestedCloudKitCodableWatch (Notification).xcscheme b/NestedCloudKitCodable.xcodeproj/xcshareddata/xcschemes/NestedCloudKitCodableWatch (Notification).xcscheme index 4c9e073..60fd2f6 100644 --- a/NestedCloudKitCodable.xcodeproj/xcshareddata/xcschemes/NestedCloudKitCodableWatch (Notification).xcscheme +++ b/NestedCloudKitCodable.xcodeproj/xcshareddata/xcschemes/NestedCloudKitCodableWatch (Notification).xcscheme @@ -56,10 +56,8 @@ allowLocationSimulation = "YES" launchAutomaticallySubstyle = "8" notificationPayloadFile = "NestedCloudKitCodableWatch Extension/PushNotificationPayload.apns"> - + - + - + - - - - - + diff --git a/NestedCloudKitCodable/Base.lproj/Main.storyboard b/NestedCloudKitCodable/Base.lproj/Main.storyboard index 55ab6aa..41d3d21 100644 --- a/NestedCloudKitCodable/Base.lproj/Main.storyboard +++ b/NestedCloudKitCodable/Base.lproj/Main.storyboard @@ -1,11 +1,9 @@ - - - - + + - + @@ -20,11 +18,54 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/NestedCloudKitCodable/ViewController.swift b/NestedCloudKitCodable/ViewController.swift index 3de670d..b763f24 100644 --- a/NestedCloudKitCodable/ViewController.swift +++ b/NestedCloudKitCodable/ViewController.swift @@ -12,6 +12,11 @@ import NestedCloudKitCodable class ViewController: UIViewController { + @IBOutlet private var resultLabel: UILabel! + @IBOutlet private var activityIndicator: UIActivityIndicatorView! + + private var schoolRecord: CKRecord? + private var director: Person { return Person(name: "Director", birthDate: Date()) } @@ -37,13 +42,7 @@ class ViewController: UIViewController { return [book1, book2] } - private var database: CKDatabase { - return CKContainer(identifier: "iCloud.com.nestedCloudKitCodable.Container").publicCloudDatabase - } - - override func viewDidLoad() { - super.viewDidLoad() - + private var school: School { var schoolObject = School() schoolObject.name = "School Name" schoolObject.location = CLLocation(latitude: 37.331274, longitude: -122.030397) @@ -51,33 +50,66 @@ class ViewController: UIViewController { schoolObject.director = director schoolObject.books = books + return schoolObject + } + + private var database: CKDatabase { + return CKContainer(identifier: "iCloud.com.nestedCloudKitCodable.Container").publicCloudDatabase + } + + @IBAction private func encodeTapped(_ sender: UIButton) { + activityIndicator.isHidden = false + activityIndicator.startAnimating() + resultLabel.text = "Loading..." + do { - let encodedRecords = try CKRecordEncoder().encode(schoolObject) + let encodedRecords = try CKRecordEncoder().encode(school) + schoolRecord = encodedRecords.last let saveOperator = CKModifyRecordsOperation(recordsToSave: encodedRecords) saveOperator.modifyRecordsCompletionBlock = { (records, recordsIDs, error) in - if let error = error { - print(error) - } else { - self.decodeSchool(encodedRecords.last!) + DispatchQueue.main.async { + self.activityIndicator.stopAnimating() + + if let error = error { + print(error) + self.resultLabel.text = "Error: \(error.localizedDescription)" + } else { + self.resultLabel.text = "Successfully encoded school object!" + } } } database.add(saveOperator) } catch let error { let formattedError = error as! CKCodableError //swiftlint:disable:this force_cast - print(formattedError) + resultLabel.text = "Error: \(formattedError.localizedDescription)" } } - private func decodeSchool(_ schoolRecord: CKRecord) { + @IBAction private func decodeTapped(_ sender: UIButton) { + guard let schoolRecord = schoolRecord else { + resultLabel.text = "Please encode the object first" + return + } + + activityIndicator.isHidden = false + activityIndicator.startAnimating() + resultLabel.text = "Loading..." + CKRecordDecoder().decode(School.self, from: schoolRecord, referenceDatabase: database) { (decodedSchool, error) in - if let error = error { - print(error) - } else if let decodedSchool = decodedSchool { - print(decodedSchool) + DispatchQueue.main.async { + self.activityIndicator.stopAnimating() + + if let error = error { + print(error) + self.resultLabel.text = "Error: \(error.localizedDescription)" + } else if let decodedSchool = decodedSchool { + self.resultLabel.text = "Successfully encoded school object!" + print(decodedSchool) + } } } } diff --git a/Podfile.lock b/Podfile.lock index a2e3457..da50b8e 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -20,4 +20,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 2ffe4c6d25cbfb6b22eb4b0a78b0a2359a4bb4f2 -COCOAPODS: 1.9.3 +COCOAPODS: 1.10.0 From bf2e579ac48b570489037e3a5ad2be4bed73c2ed Mon Sep 17 00:00:00 2001 From: Guilherme Girotto Date: Thu, 4 Feb 2021 15:23:45 -0300 Subject: [PATCH 2/3] Adds SwiftPackageManager support to the library --- NestedCloudKitCodable.xcodeproj/project.pbxproj | 2 ++ Package.swift | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 Package.swift diff --git a/NestedCloudKitCodable.xcodeproj/project.pbxproj b/NestedCloudKitCodable.xcodeproj/project.pbxproj index b2b2bb5..9656ac0 100644 --- a/NestedCloudKitCodable.xcodeproj/project.pbxproj +++ b/NestedCloudKitCodable.xcodeproj/project.pbxproj @@ -161,6 +161,7 @@ 630DDFD824F5C13300537AD5 /* NestedCloudKitCodableMacOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = NestedCloudKitCodableMacOS.entitlements; sourceTree = ""; }; 635A0E8B24F562C4002BC7AD /* NestedCloudKitCodableTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = NestedCloudKitCodableTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 635A0EF824F56516002BC7AD /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 63658A6525CC31FE0000C747 /* Package.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = ""; }; 6375386324F5347D0093936D /* NestedCloudKitCodableWatch.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NestedCloudKitCodableWatch.app; sourceTree = BUILT_PRODUCTS_DIR; }; 6375386624F5347D0093936D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Interface.storyboard; sourceTree = ""; }; 6375386824F5347E0093936D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -288,6 +289,7 @@ 607FACC71AFB9204008FA782 = { isa = PBXGroup; children = ( + 63658A6525CC31FE0000C747 /* Package.swift */, 63A249B324F567E9008C8C56 /* Source */, 63CDFCF524F5477900535338 /* NestedCloudKitCodable_Example.entitlements */, 63CDFCE424F53D3D00535338 /* Shared */, diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..8c3e536 --- /dev/null +++ b/Package.swift @@ -0,0 +1,17 @@ +import PackageDescription + +let package = Package( + name: "NestedCloudKitCodable", + platforms: [ + .macOS(.v10_13), + .iOS(.v10), + .tvOS(.v11), + .watchOS(.v3), + ], + products: [ + .library(name: "NestedCloudKitCodable", targets: ["NestedCKCodable"]) + ], + targets: [ + .target(name: "NestedCKCodable", path: "./Source") + ] +) From 5f2f6ab8017b3b3419e9d66cf85f413306e96ba5 Mon Sep 17 00:00:00 2001 From: Guilherme Girotto Date: Thu, 4 Feb 2021 17:17:08 -0300 Subject: [PATCH 3/3] Adds swift tools version to Package.swift --- Package.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Package.swift b/Package.swift index 8c3e536..42d6683 100644 --- a/Package.swift +++ b/Package.swift @@ -1,3 +1,4 @@ +// swift-tools-version:5.0 import PackageDescription let package = Package(