对于我的ios应用程序,我需要处理服务器返回错误的情况,我有兴趣处理一些错误,例如Not Found和Timed Out。
我正在使用Xamarin和Windows Azure移动服务进行开发。到目前为止,我知道如何捕获这些异常,但是,如果出现异常,我想显示一个包含刷新按钮的视图,用户可以按下该刷新按钮进行刷新(转到服务器并查看是否存在新数据,删除刷新视图,并显示新信息。)
这就是我捕获服务器抛出的异常的方式:
public async RefreshAsync(){
try
{
var results = await DailyWorkoutTable.ToListAsync();
wod = results.FirstOrDefault();
SetupUI();
}
catch(Exception e)
{
var ex = e.GetBaseException() as MobileServiceInvalidOperationException;
if(ex.Response.StatusCode == 404)
{
//this is where I need to set up the refresh view and
//and add a UIButton to it
Console.WriteLine("Daily workout not found");
}
}
}
我不知道实现这一目标的正确方法是什么。如果我创建一个UIView并向其添加一个UIButton,并且有一个再次调用RefreshAsync的事件,它将无法工作,并且不是最优雅的方式。
还有另一种方法吗?请帮忙。
答案 0 :(得分:1)
以下是一个可以作为起点的示例:
/// <summary>
/// A class for performing Tasks and prompting the user to retry on failure
/// </summary>
public class RetryDialog
{
/// <summary>
/// Performs a task, then prompts the user to retry if it fails
/// </summary>
public void Perform(Func<Task> func)
{
func().ContinueWith(t =>
{
if (t.IsFaulted)
{
//TODO: you might want to log the error
ShowPopup().ContinueWith(task =>
{
if (task.IsCompleted)
Perform(func);
}, TaskScheduler.FromCurrentSynchronizationContext());
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
/// <summary>
/// Wraps a retry/cancel popup in a Task
/// </summary>
private Task ShowPopup()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
var alertView = new UIAlertView("", "Something went wrong, retry?", null, "Ok", "Cancel");
alertView.Dismissed += (sender, e) => {
if (e.ButtonIndex == 0)
taskCompletionSource.SetResult(true);
else
taskCompletionSource.SetCanceled();
};
alertView.Show();
return taskCompletionSource.Task;
}
}
使用它:
var retryDialog = new RetryDialog();
retryDialog.Perform(() => DoSomethingThatReturnsTask());
此示例在async / await支持之前,但您可以根据需要重构它。
您可能还会考虑让Perform()返回一个Task并变为异步 - 具体取决于您的用例。