nsfetchedResultScontrollerのフェッチエラーを処理する方法は?
-
14-10-2019 - |
質問
これは、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
デリゲートディスプレイAN MFMailComposeViewController
とともに errorString
クールなクーリエフォントで:)「レポートを送信」がタップされている場合。それ以外の場合は呼び出します 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
ボタンが1つだけで(明らかにボタンにインデックス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];
}
所属していません StackOverflow