使用单独的类在Swift中为TableView处理Alamofire响应的最佳方法是什么? [英] What is the best way to handle response from Alamofire for a TableView in Swift using separate classes?
问题描述
我正在编写一个类来处理我的服务器请求,这些请求返回JSON以填充TableView。这是我第一次做这种事情,我很好奇在这里使用的最佳范例是什么。像委派这样的事情比使用dispatch_async更好吗? Alamofire的响应几乎是异步的,因此我无法从中返回数据。由于我的请求是在共享环境中发生的(它存在于我创建的框架中,所以我可以在多个目标中使用它)ServerManager类,因此我需要以某种方式将其发送到TableView,但我不确定哪种最佳方式去做。
I'm writing a class to handle my server requests that return JSON to populate a TableView. This is my first time doing this sort of thing, and I was curious what is the be best paradigm to use here. Is something like delegation better than using dispatch_async? Pretty much Alamofire's response is asynchronous, so I can't return data out of it. Since my request is happening in a shared(it exists in a framework I created so I could use it in multiple targets) ServerManager class, I need to get it to the TableView some how, and I'm not sure what the best way to do that.
委派后台线程的优点是什么,反之亦然?我知道这个问题可能在这里被问到很多,但是我在搜索时似乎找不到很好的解释。
What are the pros of delegation over background threading and vice versa? I know this question probably gets asked a lot around here, but I couldn't seem to find a good explanation when I was searching.
推荐答案
ServerManager
中的方法应传递给闭包(块)。
The method in ServerManager
should be passed a closure (block). This requires no delegation and no dispatches in the view controller.
class ServerManager {
func fetchObjectsWithOptions(options: [AnyObject], completion: (items: [AnyObject], error: ErrorType?) -> Void) {
// Use the options to setup and make the request
// Be sure it executes on the main thread
completion(items: items, error: nil)
// Any finishing needed
}
}
// ...
class MyTableViewController: UITableViewController {
lazy var serverManager = ServerManager()
var items: [AnyObject] = []
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
serverManager.fetchObjectsWithOptions([]) {
items, error in
if error == nil {
self.items = items
self.tableView.reloadData()
}
}
}
}
闭包是可以分配给变量的函数。在Swift中,闭包相对简单。
Closures are functions which can be assigned to a variables. Closures are relatively simple in Swift. Here is a closure that takes no parameters and has a void return type.
{ () -> Void in print("foo") }
下面,变量 x
的类型签名为()->无效
,并被分配了闭包。仅通过调用函数 x()
即可执行关闭操作。
Below, the variable x
has the type signature of () -> Void
, and is assigned the closure. Executing the closure is done just line calling a function, x()
.
let x: () -> Void = { () -> Void in print("foo") }
x() // prints foo
作为函数参数传递。调用 funcWithClosure()
时,它将执行闭包。
Closures can be passed around as function parameters. When funcWithClosure()
is called, it executes the closure.
func funcWithClosure(x: () -> Void) {
x()
}
funcWithClosure({ () -> Void in print("foo") })
采用参数的闭包将参数及其类型指定为闭包类型的一部分。
Closures which take parameters have the parameters and their types specified as part of the closure type.
func funcWithClosure2(x: (string: String) -> Void) {
x(string: "foo") // <-- parameters must be named
}
funcWithClosure2({ (string: String) -> Void in print(string) })
类型推断引擎允许您从闭包中删除类型。
The type inference engine allows you to remove the type from the closure.
funcWithClosure({ print("foo") }) // <-- No type declaration
funcWithClosure2({ string in print(string) }) // <-- Only parameter name
此外,如果闭包是最后一个参数,则不需要在闭包周围加上括号
In addition, if a closure is the last parameter, you don't need the parentheses around the closure.
funcWithClosure { print("foo") }
最后,这是一个以闭包结尾的多个参数的示例。
Finally, here is an example with multiple parameter ending with a closure.
func funcWithString(string: String, closure: (string: String) -> Void) {
closure(string: string)
}
funcWithString("foo", closure: { (string: String) -> Void in print(string) })
或者,您可以使用不太冗长的语法。
Or, you can use the less verbose syntax.
funcWithString("foo") { string in print(string) }
这篇关于使用单独的类在Swift中为TableView处理Alamofire响应的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!