iPhone Core Data “Production” Error Handling

前端 未结 5 1126
陌清茗
陌清茗 2020-12-04 05:52

I\'ve seen in the example code supplied by Apple references to how you should handle Core Data errors. I.e:

NSError *error = nil;
if (![context save:&err         


        
5条回答
  •  醉话见心
    2020-12-04 06:11

    I'm surprised no one here is actually handling the error the way it is meant to be handled. If you look at the documentation, you will see.

    Typical reasons for an error here include: * The device is out of space. * The persistent store is not accessible, due to permissions or data protection when the device is locked. * The store could not be migrated to the current model version. * The parent directory does not exist, cannot be created, or disallows writing.

    So if I find an error when setting up the core data stack, I swap the rootViewController of UIWindow and show UI that clearly tells the user that their device might be full, or their security settings are too high for this App to function. I also give them a 'try again' button, so they can attempt to fix the problem before the core data stack is re-attempted.

    For instance the user could free up some storage space, return to my App and press the try again button.

    Asserts? Really? Too many developers in the room!

    I'm also surprised by the number of tutorials online that don't mention how a save operation could fail for these reasons also. So you will need to ensure that any save event ANYWHERE in your App could fail because the device JUST THIS MINUTE became full with your Apps saving saving saving.

提交回复
热议问题