Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8254379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:10:06+00:00 2026-06-08T01:10:06+00:00

I am working on an application that incorporates iCloud syncing of its Core Data

  • 0

I am working on an application that incorporates iCloud syncing of its Core Data store (simple application with a single store/model/context). Since the store also contains image data, it has the possibility for getting quite large and so I would like to add a setting to allow the user to disable the syncing if they wish. I have looked at some sample code for using Core Data in both scenarios, and it looks to me that the only real difference between running with iCloud enabled and disabled are the options passed to the NSPersistentStoreCoordinator when it is added. As such, I had though about doing something like this:

NSPersistentStoreCoordinator *psc;
NSDictionary* options;
//Set options based on iCloud setting
if ([enableSwitch isOn]) {
    options = [NSDictionary dictionaryWithObjectsAndKeys:
        @"<unique name here>", NSPersistentStoreUbiquitousContentNameKey,
        cloudURL, NSPersistentStoreUbiquitousContentURLKey,
        [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
        [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
        nil];
} else {
    options = nil;
}

//Add the coordinator
if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
    //Handle error
}

So, I have a couple of questions about the above:

  1. Is my assumption correct, or is there more between the two states that needs to be different?
  2. Often in examples this code is called in the application delegate, so it usually is only called once per application run. Is there a good strategy to responding to the necessary change on demand as the user toggle the setting?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:10:07+00:00Added an answer on June 8, 2026 at 1:10 am

    This is the solution that I came up with, and it is working pretty well. Basically the code below is placed in a method that you can call when your application starts up, or anytime the your user-facing “Enable iCloud Sync” setting changes. All that needs to change between the two operation modes is the NSPersistentStore instance, the underlying model file and the coordinator need no changes.

    My application only has one persistent store to worry about, so upon calling this method, it just removes all stores currently attached to the coordinator, and then creates a new one with the appropriate options based on the user settings.

    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"DataModel.sqlite"];
    NSError *error = nil;
    
    NSArray *stores = [__persistentStoreCoordinator persistentStores];
    for (int i=0; i < [stores count]; i++) {
        [__persistentStoreCoordinator removePersistentStore:[stores objectAtIndex:i] error:&error];
    }
    
    //Determine availability.  If nil, service is currently unavailable
    NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:nil];
    if (!cloudURL) {
        NSLog(@"iCloud currently unavailable.");
    }
    
    //Check if user setting has been set
    if (![[NSUserDefaults standardUserDefaults] objectForKey:kCloudStorageKey]) {
        //Set the default based on availability
        BOOL defaultValue = (cloudURL == nil) ? NO : YES;
        [[NSUserDefaults standardUserDefaults] setBool:defaultValue forKey:kCloudStorageKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    //Set options based on availability and use settings
    BOOL cloudEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:kCloudStorageKey];
    NSDictionary *options = nil;
    
    if (cloudEnabled && cloudURL) {
        NSLog(@"Enabling iCloud Sync in Persistent Store");
        options = [NSDictionary dictionaryWithObjectsAndKeys:
                @"<awesome.unique.name.key>", NSPersistentStoreUbiquitousContentNameKey,
                cloudURL, NSPersistentStoreUbiquitousContentURLKey,
                [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                nil]; 
    }
    
    //Add the store with appropriate options
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    

    If you have an application that utilizes multiple stores, you may need to keep references around to the stores that you want to enable/disable sync on so you can have a smarter approach rather than just blowing them all away every time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with a legacy application that makes some use of the well-known/dreaded data
My group is working on a Flex/AS3 scientific web application that is data intensive.
I'm working an application that periodically fetch data from a web service. The problem
I am working on splitting out an existing, working application that I currently have
Im working on an application that needs to talk to a database. The application
I'm working on an application that needs to convert an HTML workspace (fixed size
I'm working on an application that integrates w/ Facebook to publish videos that are
I'm working on an application that contains some buttons defined via layout.xml like this
I am working on an application that imports an unmanaged dll into C#. It
I am working on an application that has been edited by various programmers over

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.