I am looking for general advice on handling CloudKit errors in Swift and am having trouble finding good examples online. Here are the things I'm wondering:
1) Should I account for every single error type each time the possibility for an error arises, or is that not really necessary?
2) I have read that one common way of handling CloudKit errors is by retrying to execute the operation after the time interval the error message provides. Should this retry basically be my standard procedure for all errors?
3) Do different CloudKit operations (save, fetch, etc.) produce different types of errors, or is there one standard set of CloudKit errors?
Thanks in advance! I'm just looking for general info on how to go about tackling error handling with CloudKit because I'm not really sure where to start.
I've written a CloudKit help that makes it much easier to process errors. This is just a starting point and there is a lot more that can be done.
The main focus of this helper, in its current state, is to make it easy to retry errors that should be retried after an appropriate timeout.
But you still need to deal with errors that shouldn't be retried such as the user's iCloud storage being full. Even with this helper, every call to one of these helper methods needs to properly handle the result and possibly report an error to the user. Of course you can add a help method that checks all of the possible error types and shows an appropriate message. Then all uses of the CloudKit code can call that one helper method.
This also only covers a few of the possible operations. You would want to add support for other operations as well. Lastly, this doesn't handle partial errors yet. That would be another useful enhancement.
Yes, you want to check every cloudkit call for errors. Apple stresses this point in the cloudkit-related WWDC videos.
What you do when you detect an error varies greatly. Retry is sometimes an option, but sometimes not appropriate. If you're using batch operations, retrying may require some additional work to extract the just the records that failed. So, yes you may sometimes want to retry, but no, you probably won't automatically retry every operation that fails.
There is one set of errors, defined in
CKError.h
. But, you don't always just get a CKError. Sometimes, especially withCKErrorPartialFailure
, you get a top-level error that contains nested errors that you also have to unwrap. As of IOS 10, the list of errors inCKError.h
looks like:One approach, while you're developing and testing the app, is to check every cloudkit operation for an error, and if detected, fire an NSAssert to stop the app. Then, examine the error, underlying errors and context to determine why it failed and what you need to do about it. Most likely, over time, you'll see common patterns emerge and you can then consider building a generic error handler.