iCloud Private Database Compare iPhone to iCloud (3/3)

Mazen Kilani
4 min readApr 6, 2021

--

Compare iPhone/iPad to iCloud Private Database and Show Difference Records on Each Platform

The Scenario

Our app will allow users to store their app records on Apple’s secure, private iCloud. The users should be able to upload, query, download, delete and/or compare their iPhone/iPad with their iCloud database records.

The Technique

We shall utilise Xcode CloudKit Dashboard interface to set up the app iCloud.com containers for Development and Production. Once setup, we can then verify that the user has authorised the iCloud account on their device, and execute operations to query, download and delete iCloud user records.

The Audience

The article is for Swift developers who seek complete, integrated, proven, code-centric solutions to speed up their development projects. The iCloud code used in this article can be added, with minimal customisation, to any part of your app.

Data Model

We based the article on the AppsGym Books model app, which is published on Apple’s App Store (as 8Books). You can review the app and also download the complete Xcode project on AppsGym.com, for free.

Multi-Part Series of Articles

The iCloud operations are relatively complex and diverse, and should cover the single record as well as batch records operations. Hence, we shall use multiple articles to segregate the different functions, starting with a series on single record scenarios, as follows:

  1. Setup, Verify, and Upload
  2. Query, Download, and Delete
  3. Compare iPhone/iPad to iCloud (this article)

Another series of articles will address iCloud batch operations, where we upload and download groups of records, simultaneously.

The User Interfaces

The AppsGym Books app uploads and downloads from Apple’s private iCloud account of the app user. This approach is a classic backup/restore function, and the app offers a comparison between iPhone and iCloud, where any differences between the iCloud remote database and the iPhone local Core Data database exist. This is typically the case if you add new records to the iPhone but did not yet upload them to iCloud, or if iCloud records have been deleted but the corresponding iPhone records remain.

The Logic

CloudTableViewController.swift, which corresponds to the Settings: iCloud main view, controls and performs the comparison logic in its 1st option: Compare iPhone to iCloud.

When the user selects the Compare iPhone to iCloud action, a pop-up alert message requests confirmation, then executes the relevant iCloud query. The app then shows the results of the query comparison in 2 scenarios: iPhone Yes iCloud No (record title exists on iPhone but not on iCloud) and iPhone No, iCloud Yes (record title does not exist on iPhone, but exists on iCloud). Naturally, you can code any other comparison criteria, such as a combined Author and Title (and/or any other) to ensure uniqueness.

We represent each of the 2 scenarios by an icon, as per the image below:

Tap the left most icon button outlet for iPhone Yes iCloud No, and the right most icon button outlet for iPhone No iCloud Yes. Each icon button action then shows a table of the records that correspond to its function. In a typical example, the screenshots below show both scenarios:

CloudCompareTableViewController.swift controls the table views of both scenarios, the view Title changes as per the scenario presented.

If the query of ‘ iPhone Yes iCloud No ‘ shows records, then you need to upload these records from iPhone to iCloud.

If the query of ‘ iPhone No iCloud Yes ‘ shows records, then, should you wish, you can download these records from iCloud to iPhone.

The complete code assumes you

  • have already set up the app’s iCloud Private Database (using CloudKit) and
  • checked the user’s iCloud account is set up correctly for query, uploads, and downloads.

Otherwise, the app logic presents an error alert. For the complete iCloud setup and query, upload and download logic, please refer to the previous 2 Medium articles and/or download the AppsGym Books Xcode project template, totally free and/or review the AppsGym iCloud Tutorials.

The Code

The Swift code snippets, specifically relevant to iPhone vs iCloud comparison are below.

CloudTableViewController.swift Initialise Variables

CloudTableViewController.swift viewDidLoad()

CloudTableViewController.swift Setup Message and Switches

CloudTableViewController.swift requestConfirmationToCompare()

CloudTableViewController.swift compareCloudToDeviceAfterConfirmation()

CloudTableViewController.swift startCompareMessages()

CloudTableViewController.swift compareCloudToDevice()

CloudTableViewController.swift setupCompareArrays()

CloudTableViewController.swift compareCloudToDevice()

CloudTableViewController.swift compareDeviceToCloud()

CloudTableViewController isICloudSetupOK()

We shall now show the differences table view.

CloudCompareTableViewController.swift

The 3rd and final article of our iCloud Private Database coverage concluded with the facility to compare current iPhone/iPad records with the iCloud Private Database records, then showing a table view of the differences. Hope you find it useful in your project. Thanks for reading!

--

--

Mazen Kilani
Mazen Kilani

Written by Mazen Kilani

I published 47 Swift iOS apps/games and 2 Flutter Android apps. I share complete Xcode projects, free (no ads), at AppsGym.com.

No responses yet