Search code examples
iosswiftcore-datasaveicloud

Check if iCloud Account is On and write it to Core Data


I know how to check if iCloud is On:

if var transactionLogsURL:NSURL = fileManager.URLForUbiquityContainerIdentifier(nil){
ON...}else{OFF..}

but in lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {}() I can't pass var context:NSManagedObjectContext = managedObjectContext! and I can't add it to CoreData is it on or is it off. Any suggestions ?


Solution

  • Here's my suggestion: Do not write the iCloud status to your persistent store. Users can enable or disable iCloud at any time. It doesn't make sense to store the current value, because that might be wrong the next time you want to know.

    Instead, check the iCloud status when the app starts up, and observe notifications that the iCloud status has changed. The status is inherently volatile data, it doesn't make sense to save it to persistent storage.