@@ -12,6 +12,11 @@ import NestedCloudKitCodable
1212
1313class ViewController : UIViewController {
1414
15+ @IBOutlet private var resultLabel : UILabel !
16+ @IBOutlet private var activityIndicator : UIActivityIndicatorView !
17+
18+ private var schoolRecord : CKRecord ?
19+
1520 private var director : Person {
1621 return Person ( name: " Director " , birthDate: Date ( ) )
1722 }
@@ -37,47 +42,74 @@ class ViewController: UIViewController {
3742 return [ book1, book2]
3843 }
3944
40- private var database : CKDatabase {
41- return CKContainer ( identifier: " iCloud.com.nestedCloudKitCodable.Container " ) . publicCloudDatabase
42- }
43-
44- override func viewDidLoad( ) {
45- super. viewDidLoad ( )
46-
45+ private var school : School {
4746 var schoolObject = School ( )
4847 schoolObject. name = " School Name "
4948 schoolObject. location = CLLocation ( latitude: 37.331274 , longitude: - 122.030397 )
5049 schoolObject. students = students
5150 schoolObject. director = director
5251 schoolObject. books = books
5352
53+ return schoolObject
54+ }
55+
56+ private var database : CKDatabase {
57+ return CKContainer ( identifier: " iCloud.com.nestedCloudKitCodable.Container " ) . publicCloudDatabase
58+ }
59+
60+ @IBAction private func encodeTapped( _ sender: UIButton ) {
61+ activityIndicator. isHidden = false
62+ activityIndicator. startAnimating ( )
63+ resultLabel. text = " Loading... "
64+
5465 do {
55- let encodedRecords = try CKRecordEncoder ( ) . encode ( schoolObject)
66+ let encodedRecords = try CKRecordEncoder ( ) . encode ( school)
67+ schoolRecord = encodedRecords. last
5668
5769 let saveOperator = CKModifyRecordsOperation ( recordsToSave: encodedRecords)
5870 saveOperator. modifyRecordsCompletionBlock = { ( records, recordsIDs, error) in
59- if let error = error {
60- print ( error)
61- } else {
62- self . decodeSchool ( encodedRecords. last!)
71+ DispatchQueue . main. async {
72+ self . activityIndicator. stopAnimating ( )
73+
74+ if let error = error {
75+ print ( error)
76+ self . resultLabel. text = " Error: \( error. localizedDescription) "
77+ } else {
78+ self . resultLabel. text = " Successfully encoded school object! "
79+ }
6380 }
6481 }
6582
6683 database. add ( saveOperator)
6784 } catch let error {
6885 let formattedError = error as! CKCodableError //swiftlint:disable:this force_cast
69- print ( formattedError)
86+ resultLabel . text = " Error: \ ( formattedError. localizedDescription ) "
7087 }
7188 }
7289
73- private func decodeSchool( _ schoolRecord: CKRecord ) {
90+ @IBAction private func decodeTapped( _ sender: UIButton ) {
91+ guard let schoolRecord = schoolRecord else {
92+ resultLabel. text = " Please encode the object first "
93+ return
94+ }
95+
96+ activityIndicator. isHidden = false
97+ activityIndicator. startAnimating ( )
98+ resultLabel. text = " Loading... "
99+
74100 CKRecordDecoder ( ) . decode ( School . self,
75101 from: schoolRecord,
76102 referenceDatabase: database) { ( decodedSchool, error) in
77- if let error = error {
78- print ( error)
79- } else if let decodedSchool = decodedSchool {
80- print ( decodedSchool)
103+ DispatchQueue . main. async {
104+ self . activityIndicator. stopAnimating ( )
105+
106+ if let error = error {
107+ print ( error)
108+ self . resultLabel. text = " Error: \( error. localizedDescription) "
109+ } else if let decodedSchool = decodedSchool {
110+ self . resultLabel. text = " Successfully encoded school object! "
111+ print ( decodedSchool)
112+ }
81113 }
82114 }
83115 }
0 commit comments