Classes
The following classes are available globally.
-
An operation that fetches data from CloudKit and saves it to Core Data, you can use it without calling
See moreCloudCore.fetchAndSave
methods if you application relies onOperation
Declaration
Swift
public class FetchAndSaveOperation: Operation
-
CloudCore’s class for storing global
CKToken
objects. Framework uses one to upload or download only changed data (smart-sync).To detect what data is new and old, framework uses CloudKit’s
CKToken
objects and it is needed to be loaded every time application launches and saved on exit.Framework stores tokens in 2 places:
- singleton
Tokens
object inCloudCore.tokens
- tokens per record inside Record Data attribute, it is managed automatically you don’t need to take any actions about that token
You need to save
Tokens
object before application terminates otherwise you will loose smart-sync ability.Example
See morefunc applicationWillTerminate(_ application: UIApplication) { CloudCore.tokens.saveToUserDefaults() }
Declaration
Swift
open class Tokens: NSObject, NSCoding
- singleton
-
Main framework class, in most cases you will use only methods from this class, all methods and properties are
static
.Save to cloud
On application inialization call
CloudCore.enable(persistentContainer:)
method, so framework will automatically monitor changes at Core Data and upload it to iCloud.Example
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Register for push notifications about changes application.registerForRemoteNotifications() // Enable CloudCore syncing CloudCore.delegate = someDelegate // it is recommended to set delegate to track errors CloudCore.enable(persistentContainer: persistentContainer) return true }
Fetch from cloud
When CloudKit data is changed push notification is posted to an application. You need to handle it and fetch changed data from CloudKit with
CloudCore.fetchAndSave(using:to:error:completion:)
method.Example
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { // Check if it CloudKit's and CloudCore notification if CloudCore.isCloudCoreNotification(withUserInfo: userInfo) { // Fetch changed data from iCloud CloudCore.fetchAndSave(using: userInfo, to: persistentContainer, error: nil, completion: { (fetchResult) in completionHandler(fetchResult.uiBackgroundFetchResult) }) } }
You can also check for updated data at CloudKit manually (e.g. push notifications are not working). Use for that
See moreCloudCore.fetchAndSave(to:error:completion:)
Declaration
Swift
open class CloudCore