English 中文(简体)
如何在进行节约时处理错误 反对案文?
原标题:how to handle error when saving managedObjectContext?

What s the appropriate strategy for restoring to the existing persistent store if there s an error when doing a -[NSManagedObjectContext save:] ?

- [国家管理”

或者,在试图挽救之前,我是否应当把存放在档案系统上:如果出现错误,则使用——[NSPersistentStoreCo Coordinator setURL:forPersistentStore:]将储存放到所节约的仓库?

感谢任何要点。

Gonzalo

最佳回答

当储蓄失败时,你(作为方案者)就错了。

我撰写了一些极为复杂的核心-Data申请,从来不看到生产中的失败。

保证用户能够进入这些数值,可以不发生任何错误。

This means if you have a not-optional relationship don t accept the "Done"-button until the user has selected an entity for this relationship. Or set a default relationship entity.
If you need a NSString in a special format validate that string before accepting it.

It s more convenient for the user too. If you tell him 10 minutes ago you did something wrong, thats why I will delete all your changes he will think WTF?. If you tell him The value you try to enter is not in the correct format. Please use xxxxx he will think Ok, let s change it.

And now after you made sure that a save never fails you can put [context rollback] in it, because you should never say never.
But before you do this you should ask the user if he wants to send you the error log. Because a save should never fail.

问题回答

我认为,尽管我看不出在docs中明确写法,但如果在储蓄期间出现错误,那么储蓄就会失败,数据储存将保持不变。 您可以通过复制数据储存来方便地加以核实,从而造成某种错误,然后将储存与复制进行比较。

NSManagedContext save returns a BOOL, YES if successful, NO if not. That will tell you for sure whether the data store has actually been changed.

Another thing: Take a look at Apple s "Core Data Programming Guide: Persistent Store Features," under "Configuring a SQLite Store’s Save Behavior." They give a complete example of how to set up full sync options for the store, to ensure against data corruption due to some interruption during the save process.





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签