对于资深和有经验的开发人员来说,这可能是一个显而易见的问题,所以我真的很害怕问它。
假设我有一个移动应用程序,它为不同的数据模型调用了太多的 RESTful API。假设有可以在这些模型上进行 CRUD 的 ViewController A、B、C。现在有几件事可能会出错。每个调用都有一个失败块。我正在使用Strongloop/Loopback ios sdk。
静默忽略如下错误的策略:
[repo logoutWithSuccess:^(){
// ... success code
} failure:CALLBACK_FAILURE_BLOCK];
已将CALLBACK_FAILURE_BLOCK
定义为
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#define CALLBACK_FAILURE_BLOCK
^(NSError *error) {
ALog("Callback failed: %@", error.description);
}
在一个理想的世界中,每个客户端验证都完美运行,并且可以将头埋在沙子里,这将是很棒的。
目前,我已经在每个 ViewController 中定义了一个帮助程序函数,当这些调用失败时向用户显示警报。
[self.trip saveWithSuccess:^(){
//... success code
} failure:^(NSError *error){
[self displayErrorWithMessage:[error localizedDescription]
andTitle:NSLocalizedString(@"Service Error", @"Server Error")];
}];
和
- (void)displayErrorWithMessage:(NSString*)msg andTitle:(NSString*)title{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:title
message:msg
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
我正在考虑将此辅助程序函数重构为位于公共位置,例如 AppDelegate(可能在协议中)。
这是一个正确的策略吗,您是否还有其他最佳实践来更好地设计它?
最近,我将这种方法用于我的一个应用程序,我希望它运行良好且易于处理错误。
为此,我创建了UIViewController的ErrorViewController子类(在我的例子中它是VCBaseViewController,它是所有其他视图控制器的父类,它是UIViewController的子类)。 您可以使用故事板或 XIB,这取决于您。 我在整个应用程序中使用了故事板。
My ErrorViewController.h
@interface ErrorViewController : VCBaseViewController
@property (nonatomic, strong) NSString *errorTitle;
@property (nonatomic, strong) NSString *errorDescription;
@property(nonatomic,assign) ErrorPageType errorPage;
@property (weak, nonatomic) IBOutlet UILabel *lblErrorText;
@property (weak, nonatomic) IBOutlet UITextView *textViewErrorString;
@property (weak, nonatomic) IBOutlet UIButton *btnAction;
@end
ErrorViewController.m
@interface ErrorViewController ()
@end
@implementation错误视图控制器
- (void)viewDidLoad {
[super viewDidLoad];
[self configureNavigationBarWithStyle:NavigationStyleBackButton];
[self setViewControllerTitle:@"Error View"];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
switch (self.errorPage)
{
case ErrorTypeNetworkError:
{
}
break;
case ErrorTypeTechnicalError:
{
self.lblErrorText.text = @"Technical Error";
self.textViewErrorString.text = @"Due to some technical errorn page not exist";
}
break;
default:
{
self.lblErrorText.text = self.errorTitle;
self.textViewErrorString.text = self.errorDescription;
}
break;
}
}
- (IBAction)btnErrorAction:(id)sender{
[self errorPageAction];
}
- (void)backButtonPressed{
[self errorPageAction];
}
- (void)errorPageAction
{
switch (self.errorPage)
{
case ErrorTypeNetworkError:
{
}
break;
case ErrorTypeProductNotAvailable:
{
[self popToPreviousController];
}break;
default:
{
[self popToPreviousController];
}break;
}
}
- (void)popToPreviousController
{
NSArray* viewcontrollers = [self.navigationController viewControllers];
if(viewcontrollers.count>2)
{
int index = (int)viewcontrollers.count - 3;
[self.navigationController popToViewController:[viewcontrollers objectAtIndex:index] animated:YES];
}else{
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
我的故事板设计如下所示,其中包含错误图像错误标签以及文本视图和后退按钮,因为我使用的是导航控制器。[![在此输入图像描述][1]][1]
my BaseViewController.h
typedef enum {
ErrorTypeNetworkError = 0,
ErrorTypeTechnicalError,
ErrorTypeNone
} ErrorPageType;
- (void)pushErrorViewcontroller:(NSError*)error;
- (void)pushErrorViewcontroller:(NSString*)errorTitle andErrorPage:(ErrorPageType)errorType;
- (void)pushErrorViewcontroller:(NSString *)errorTitle errorDescription:(NSString *)errorDescription errorType:(ErrorPageType)errorType;
BaseViewController.m
- (void)pushErrorViewcontroller:(NSString*)errorTitle andErrorPage:(ErrorPageType)errorType{
[self pushErrorViewcontroller:nil errorDescription:nil errorType:errorType];
}
- (void)pushErrorViewcontroller:(NSError*)error{
[self pushErrorViewcontroller:nil errorDescription:[error localizedDescription] errorType:ErrorTypeNone];
}
- (void)pushErrorViewcontroller:(NSString *)errorTitle errorDescription:(NSString *)errorDescription errorType:(ErrorPageType)errorType{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
ErrorViewController *errorViewController = [storyBoard instantiateViewControllerWithIdentifier:NSStringFromClass([ErrorViewController class])];
errorViewController.errorPage = errorType;
errorViewController.errorDescription = errorDescription;
errorViewController.errorTitle = errorTitle;
[self.navigationController pushViewController:errorViewController animated:YES];
}
我像这样在其他视图控制器中调用错误视图控制器方法。
- (void) ServiceCallForOrderHistoryWithCurrentPageMark: (NSString *)page
{
__block __weak VCMyOrderViewController* blockMyOrderListView = self;
[[ModelManager sharedInstance] getMyOrderDetail:[[Configuration sharedConfig] getLoggedInUserId] andPageNumber:page completionBlock:^(id result, NSError *error)
{
if (!error)
{
blockMyOrderListView.objOrderHistoryBaseClass = (OrderHistoryBaseClass *)result;
if (blockMyOrderListView.objOrderHistoryBaseClass.rESPONSE.totalOrderCount.intValue == 0)
{
[blockMyOrderListView.tblMyOrder setHidden:YES];
[blockMyOrderListView.viewNoOrders setHidden:NO];
}
else
{
[blockMyOrderListView setDataSourceMyOrders:blockMyOrderListView.objOrderHistoryBaseClass.rESPONSE];
[blockMyOrderListView.tblMyOrder reloadData];
}
}
else
{
[blockMyOrderListView pushErrorViewcontroller:error];
}
}];
}