gpt4 book ai didi

iphone - 如何处理 NSFetchedResultsController 获取错误?

转载 作者:技术小花猫 更新时间:2023-10-29 11:04:50 24 4
gpt4 key购买 nike

这是来自 Apple 示例代码:

if (![fetchedResultsController_ performFetch:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
...
If it is not possible to recover from the error, ...
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

我想知道是否真的有必要总是终止应用程序?您如何“用代码替换此实现以适本地处理错误”?您将如何“从错误中恢复”?

任何建议,法比安,我们将不胜感激

最佳答案

好吧,显然没有人有其他(更好的?)解决方案,所以这是我的方法:

在我的 AppController 中,我添加了一个实例变量 errorString 和这个方法:

- (void)presentCoreDataError:(NSError *)error
withText:(NSString *)text
{
NSMutableString *localErrorString = [[NSMutableString alloc] init];

[localErrorString appendFormat:@"Failed to %@: %@", text, [error localizedDescription]];

NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors != nil && [detailedErrors count] > 0) {
for(NSError* detailedError in detailedErrors) {
[localErrorString appendFormat:@"- Detail: %@", [detailedError userInfo]];
}
} else {
[localErrorString appendFormat:@"- %@", [error userInfo]];
}

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Failed to %@", text]
message:@"Please send a report to the developer."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Send Report", nil] autorelease];
[alert show];

self.errorString = localErrorString;
[localErrorString release];
}

如果点击“发送报告”,UIAlertView 委托(delegate)会显示一个带有 errorStringMFMailComposeViewController,采用炫酷的 courier 字体 :)。否则它调用 abort():

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) { // Send Report
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];
[picker setToRecipients:toRecipients];
[picker setSubject:@"Error Report"];
[picker setMessageBody:[NSString stringWithFormat:@"The application crashed with the following error:<br><br><FONT FACE=%@> %@ </FONT>",
@"courier", errorString]
isHTML:YES];

[navigationController presentModalViewController:picker animated:YES];
[picker release];
} else {
abort();
}
}

MFMailComposeViewControllerDelegate 显示第二个只有一个按钮的 UIAlertView(显然按钮的索引为 0,因此它将调用 abort() ):

- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error
{
[navigationController dismissModalViewControllerAnimated:YES];

NSMutableString *messageString = [[NSMutableString alloc] init];

if (result == MFMailComposeResultSent) {
[messageString appendFormat:@"Thanks! "];
}

[messageString appendFormat:@"The application has to quit now."];
UIAlertView *abortAlert = [[[UIAlertView alloc] initWithTitle:nil
message:messageString
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease];

[abortAlert show];

[messageString release];
}

关于iphone - 如何处理 NSFetchedResultsController 获取错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4556664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com