显示来自非UI类的警报 [英] Display alert from a non-UI class

查看:88
本文介绍了显示来自非UI类的警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用Alamofire,希望在请求有错误(例如错误的URL)等时显示警报。

I'm using Alamofire in my application and wish to display an alert if the request has an error (e.g. wrong URL), etc.

我具有此功能

    Alamofire.request(.GET, api_url)
        .authenticate(user: str_api_username, password: str_api_password)
        .validate(statusCode: 200..<300)
        .response { (request, response, data, error) in

            if (error != nil) {
                let alertController = UIAlertController(title: "Server Alert", message: "Could not connect to API!", preferredStyle: UIAlertControllerStyle.Alert)
                alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
                self.presentViewController(alertController, animated: true, completion: nil)
            }
    }

作为Alamofire w我需要异步进行错误检查,然后&那里(除非您另有建议),因为那时我想操纵结果,如果URL错误,那么它可能会变得凌乱。

As Alamofire works asynchronously I need to do the error check then & there (unless you suggest otherwise) because then I want to manipulate the results and if the URL was wrong then it can get messy.

毫无疑问,

self.presentViewController(alertController, animated: true, completion: nil)

不起作用,如何显示此警报?

does not work so how can I display this alert?

推荐答案

I要说的传统方法是让呼叫此网络请求的人负责显示警报。如果请求完成后,您将回调到原始调用对象,它们将负责显示警报。原因之一是错误在不同的上下文中可能意味着不同的事物。您可能并不总是希望显示警报-在构建应用程序时,这为您提供了更大的灵活性。与AlamoFire完成后调用响应关闭的方式相同,我认为最好将其传递回在您的 Downloader 对象中发起此调用的人。

I'd say the conventional approach for this is to have whoever calls this network request be responsible for displaying the alert. If when the request is complete, you call back to the original calling object, they are responsible for displaying the alert. One of the reasons for this is that errors can mean different things in different contexts. You may not always want to display an alert - this provides you with more flexibility as you're building out your app. The same way that AlamoFire calls your response closure when it is done, I think it's best to pass that back to whoever initiated this call in your Downloader object.

更新:
您希望使用AlamoFire的构建方式进行构建。您将闭包传递给AF,当AF请求完成时,AF将被调用。

Update: You want to structure it the same way AlamoFire structures it. You pass the closure to AF which gets called when the AF request finishes.

您必须在下载函数中添加一个闭包参数(请参见 downloadMyStuff )。然后,一旦AF请求完成,您就可以调用先前定义的闭包( completion )。这是一个简单的示例

You'll have to add a closure param to your download function (See downloadMyStuff). Then, once the AF request finishes, you can call the closure you previously defined ( completion). Here's a quick example

class Downloader {
  func downloadMyStuff(completion: (AnyObject?, NSError?) -> Void) {

    Alamofire.request(.GET, "http://myapi.com")
        .authenticate(user: "johndoe", password: "password")
        .validate(statusCode: 200..<300)
        .response { (request, response, data, error) in
            completion(data, error)

      }
  }
}

class ViewController: UIViewController {

let downloader = Downloader()

override func viewDidLoad() {
    super.viewDidLoad()

    self.downloader.downloadMyStuff { (maybeResult, maybeError) -> Void in
        if let error = maybeError {
            println("Show your alert here from error \(error)")
        }

        if let result: AnyObject = maybeResult {
            println("Parse your result and do something cool")
        }
    }

  }
}

这篇关于显示来自非UI类的警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆