Introduction - Learning iCloud Data Management (2014)

Learning iCloud Data Management (2014)

Introduction

Cocoa and Cocoa Touch consist of frameworks that contain classes as well as protocols, defined constants, and some other supporting items including dynamic sharable libraries. The most basic frameworks are Foundation, UIKit (Cocoa Touch—iOS) and AppKit (Cocoa—OS X). More specialized frameworks, such as the Core Audio Kit Framework, are used as needed by developers.

iCloud is different. Don’t search for an iCloud framework: there is none. Don’t even search for an iCloud API. There are a couple iCloud-specific methods, but they are few and far between. In fact, they’re very far between in the sense that they are scattered across various classes and frameworks. URLForPublishingUbiquitousItemAtURL:expirationDate:error: is part of the NSFileManager class (there are seven iCloud-related methods among the 52 methods in this class), whileNSPersistentStoreDidImportUbiquitousContentChangesNotification is part of the NSPersistentStoreCoordinator class (it is one of two iCloud-related notifications in this class).

The implementation of iCloud in this way means that existing apps that don’t use iCloud aren’t affected. In addition, because iCloud spans multiple devices as well as both operating systems (OS X and iOS), it is hard to imagine how it could have been implemented in a single framework or API.

Along with these few additions to the Cocoa and Cocoa Touch APIs, the implementation of iCloud relies on long-time best practices, which now have been converted to essential practices. Design patterns such as key-value coding that date back to the very early versions of NeXTSTEP have been used for a quarter of a century now, and they are used in new ways in iCloud, although in most cases you don’t have to do anything to take advantage of the iCloud functionality.

Core Data, which has long been the most powerful solution to managing an app’s persistent data, is deeply integrated with iCloud. However, that integration is largely (but not totally) done behind the scenes. If you don’t use iCloud, your existing Core Data code is just fine. Perhaps the most significant impact of iCloud on Core Data is that, in the past, there were two ways of creating a data store that could be distributed with an app. You could place a seed database in the app’s bundle, or you could add seed data programmatically to an empty data store that you create the first time the app launches (or whenever the seed data needs to be recreated). Both techniques have been used for years. The biggest impact that iCloud has on Core Data is that with iCloud, the second technique needs to be used; the first one will not work properly. This is scarcely a major change.

Perhaps the most visible impact of iCloud on developers is the enhancement of entitlements that control what an app can do in its runtime environment. Entitlements implement the new sandboxing rules that come into play with shared documents on iCloud. Explicit entitlements and sandboxing define the functions and capabilities of the operating system that an app will use along with the specific parts of disk storage where the app can write data. They increase the stability and security of both operating systems. They are required on iOS and are optional on OS X. On both operating systems, they are more aggressively implemented. Furthermore, from a developer’s point of view, you’ll probably be happy to hear that the developer-facing interface for entitlements in Xcode 5 is now vastly changed and dramatically simplified. (Sandboxing is related to iCloud, but they are two separate functionalities.)

The implementation of iCloud has proceeded over several years; in mid-2012, the release of OS X Mountain Lion (10.8) and iOS 6 brought together some of the pieces that had been released over the previous year. In the fall of 2013, OS X Mavericks (10.9) and iOS 7 refined iCloud and expanded its behind-the-scenes tools for developers. If you have not used any of the iWork apps (Numbers, Pages, and Keynote), try one of them on multiple iCloud-enabled devices. They provide the best demonstration of iCloud from the user’s point of view.

Actually, that statement is wrong. They provide the best demonstration of iCloud from the user’s point of view—until you write your iCloud-enabled app.