CloudCore

open class CloudCore

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 CloudCore.fetchAndSave(to:error:completion:)

  • CloudCore configuration, it’s recommended to set up before calling any of CloudCore methods. You can read more at CloudCoreConfig struct description

    Declaration

    Swift

    public static var config = CloudCoreConfig()
  • Tokens object, read more at class description. By default variable is loaded from User Defaults.

    Declaration

    Swift

    public static var tokens = Tokens.loadFromUserDefaults()
  • Error and sync actions are reported to that delegate

    Declaration

    Swift

    public static weak var delegate: CloudCoreDelegate?
  • Enable CloudKit and Core Data synchronization

    Declaration

    Swift

    public static func enable(persistentContainer container: NSPersistentContainer)

    Parameters

    container

    NSPersistentContainer that will be used to save data

  • Disables synchronization (push notifications won’t be sent also)

    Declaration

    Swift

    public static func disable()
  • Fetch changes from one CloudKit database and save it to CoreData from didReceiveRemoteNotification method.

    Don’t forget to check notification’s UserInfo by calling isCloudCoreNotification(withUserInfo:). If incorrect user info is provided FetchResult.noData will be returned at completion block.

    Declaration

    Swift

    public static func fetchAndSave(using userInfo: NotificationUserInfo, to container: NSPersistentContainer, error: ErrorBlock?, completion: @escaping (_ fetchResult: FetchResult) -> Void)

    Parameters

    userInfo

    notification’s user info, CloudKit database will be extraced from that notification

    container

    NSPersistentContainer that will be used to save fetched data

    error

    block will be called every time when error occurs during process

    completion

    FetchResult enumeration with results of operation

  • Fetch changes from all CloudKit databases and save it to Core Data

    Declaration

    Swift

    public static func fetchAndSave(to container: NSPersistentContainer, error: ErrorBlock?, completion: (() -> Void)?)

    Parameters

    container

    NSPersistentContainer that will be used to save fetched data

    error

    block will be called every time when error occurs during process

    completion

    FetchResult enumeration with results of operation

  • Check if notification is CloudKit notification containing CloudCore data

    Declaration

    Swift

    public static func isCloudCoreNotification(withUserInfo userInfo: NotificationUserInfo) -> Bool

    Parameters

    userInfo

    userInfo of notification

    Return Value

    true if notification contains CloudCore data