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
CloudCoreConfigstruct descriptionDeclaration
Swift
public static var config = CloudCoreConfig() -
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
containerNSPersistentContainerthat 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
didReceiveRemoteNotificationmethod.Don’t forget to check notification’s UserInfo by calling
isCloudCoreNotification(withUserInfo:). If incorrect user info is providedFetchResult.noDatawill 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
userInfonotification’s user info, CloudKit database will be extraced from that notification
containerNSPersistentContainerthat will be used to save fetched dataerrorblock will be called every time when error occurs during process
completionFetchResultenumeration 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
containerNSPersistentContainerthat will be used to save fetched dataerrorblock will be called every time when error occurs during process
completionFetchResultenumeration with results of operation -
Check if notification is CloudKit notification containing CloudCore data
Declaration
Swift
public static func isCloudCoreNotification(withUserInfo userInfo: NotificationUserInfo) -> BoolParameters
userInfouserInfo of notification
Return Value
trueif notification contains CloudCore data
CloudCore Class Reference