Prevent CloudKit sync / trigger sync manually

CloudKit sync with Core Data is quite an energy expensive task which can drain the battery quickly.

As far as I found out, CloudKit triggers a sync when it thinks it's convenient to do so. I discovered, that in my app the sync is triggered almost every time I save the context, which is quite often.

In my case it's only necessary to sync after certain events happened and not every time I save the context. So it's quite an energy waste right now. Since I am not able to reduce the amount of context saves, I try to Implement a logic that enables me to prevent the sync until I release it again.

So far I was not able to find any built in methods that would allow me to do that. Any Ideas how I could implement that? Is there a way to temporarily disable CloudKit completely without harm?

Answered by newwbee in 723967022

Short term approaches

1. User to turn off iCloud manually

The user of the app can turn off iCloud for the App by doing the following on iOS:

  1. Settings > iCloud > YourAppName
  2. You will see a toggle switch to turn off iCloud usage for the app

Every user will have to do that by himself / herself manually

2. CloudKit Dashboard (turn off visibility)

Warning: Please test using a dummy container before you turn off your real container's visibility

  1. Go to CloudKit Dashboard
  2. Tap on the container name, a drop down would show up
  3. Tap on Manage Containers
  4. Warning: Please test using another container before turning off visibility on the real production container

Long term approaches

In the long term, you could try one of the following which ever suits you best

1. Separate Entity that doesn't sync automatically

On your CoreData Model, create a separate configuration and add an entity to that configuration.

Do all your saving into the new entity (which doesn't sync automatically), when you are ready move it to another entity which uses the default configuration which would sync.

You need to make changes to your store descriptions and new store description for that new configuration.

Refer: https://developer.apple.com/wwdc19/202

2. Don't save view context directly, instead use a child context (not 100% sure)

If you save the viewContext, it would save to file and would trigger a sync, instead create a child context to the view context and save it in the child context

That way your views will reflect the change but it is not saved to disk, but you need to save it before app quits or leaves a view so that user doesn't loose the data.

3. Change your logic and see how you group records and then save together

Accepted Answer

Short term approaches

1. User to turn off iCloud manually

The user of the app can turn off iCloud for the App by doing the following on iOS:

  1. Settings > iCloud > YourAppName
  2. You will see a toggle switch to turn off iCloud usage for the app

Every user will have to do that by himself / herself manually

2. CloudKit Dashboard (turn off visibility)

Warning: Please test using a dummy container before you turn off your real container's visibility

  1. Go to CloudKit Dashboard
  2. Tap on the container name, a drop down would show up
  3. Tap on Manage Containers
  4. Warning: Please test using another container before turning off visibility on the real production container

Long term approaches

In the long term, you could try one of the following which ever suits you best

1. Separate Entity that doesn't sync automatically

On your CoreData Model, create a separate configuration and add an entity to that configuration.

Do all your saving into the new entity (which doesn't sync automatically), when you are ready move it to another entity which uses the default configuration which would sync.

You need to make changes to your store descriptions and new store description for that new configuration.

Refer: https://developer.apple.com/wwdc19/202

2. Don't save view context directly, instead use a child context (not 100% sure)

If you save the viewContext, it would save to file and would trigger a sync, instead create a child context to the view context and save it in the child context

That way your views will reflect the change but it is not saved to disk, but you need to save it before app quits or leaves a view so that user doesn't loose the data.

3. Change your logic and see how you group records and then save together

Prevent CloudKit sync / trigger sync manually
 
 
Q