使用AlamoFire框架后,我注意到完成处理程序在主线程上运行。我想知道下面的代码是否是在完成处理程序中创建Core Data导入任务的好习惯:
Alamofire.request(.GET, "http://myWebSite.com", parameters: parameters)
.responseJSON(options: .MutableContainers) { (_, _, JSON, error) -> Void in
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { () -> Void in
if let err = error{
println("Error:\(error)")
return;
}
if let jsonArray = JSON as? [NSArray]{
let importer = CDImporter(incomingArray: jsonArray entity: "Artist", map: artistEntityMap);
}
});
}
这是一个非常好的问题。您的方法是完全有效的。但是,Alamofire实际上可以帮助您进一步简化此流程。
在示例代码中,您将在以下调度队列之间跳转:
如您所见,您到处都是。让我们看看利用Alamofire内部强大功能的另一种方法。
Alamofire在其自身的低级处理中内置了一种最佳方法。response
如果您选择使用该方法,则最终由所有自定义响应序列化程序调用的单个方法将支持自定义调度队列。
尽管GCD在调度队列之间跳转非常出色,但您要避免跳转到繁忙的队列(例如主线程)。通过消除html" target="_blank">异步处理过程中跳回主线程的方式,您可以大大加快处理速度。以下示例演示了如何直接使用Alamofire逻辑来执行此操作。
let queue = dispatch_queue_create("com.cnoon.manager-response-queue", DISPATCH_QUEUE_CONCURRENT)
let request = Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
request.response(
queue: queue,
serializer: Request.JSONResponseSerializer(options: .AllowFragments),
completionHandler: { _, _, JSON, _ in
// You are now running on the concurrent `queue` you created earlier.
println("Parsing JSON on thread: \(NSThread.currentThread()) is main thread: \(NSThread.isMainThread())")
// Validate your JSON response and convert into model objects if necessary
println(JSON)
// To update anything on the main thread, just jump back on like so.
dispatch_async(dispatch_get_main_queue()) {
println("Am I back on the main thread: \(NSThread.isMainThread())")
}
}
)
let queue = dispatch_queue_create("com.cnoon.manager-response-queue", DISPATCH_QUEUE_CONCURRENT)
let request = Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
request.response(
queue: queue,
responseSerializer: Request.JSONResponseSerializer(options: .AllowFragments),
completionHandler: { response in
// You are now running on the concurrent `queue` you created earlier.
print("Parsing JSON on thread: \(NSThread.currentThread()) is main thread: \(NSThread.isMainThread())")
// Validate your JSON response and convert into model objects if necessary
print(response.result.value)
// To update anything on the main thread, just jump back on like so.
dispatch_async(dispatch_get_main_queue()) {
print("Am I back on the main thread: \(NSThread.isMainThread())")
}
}
)
let queue = DispatchQueue(label: "com.cnoon.response-queue", qos: .utility, attributes: [.concurrent])
Alamofire.request("http://httpbin.org/get", parameters: ["foo": "bar"])
.response(
queue: queue,
responseSerializer: DataRequest.jsonResponseSerializer(),
completionHandler: { response in
// You are now running on the concurrent `queue` you created earlier.
print("Parsing JSON on thread: \(Thread.current) is main thread: \(Thread.isMainThread)")
// Validate your JSON response and convert into model objects if necessary
print(response.result.value)
// To update anything on the main thread, just jump back on like so.
DispatchQueue.main.async {
print("Am I back on the main thread: \(Thread.isMainThread)")
}
}
)
这是此方法涉及的不同调度队列的细分。
通过消除返回主调度队列的第一跳,您消除了潜在的瓶颈,并使整个请求和处理成为异步。太棒了!
话虽如此,我对强调Alamofire真正工作原理的内在知识有多么重要。您永远不知道什么时候可以找到真正可以帮助您改进自己的代码的东西。
我的完成处理程序有问题。下面是一个带有完成处理程序的函数,位于一个实用程序文件中: 我在视图控制器中调用它 输出清楚地表明该函数在运行该块之前没有等待完成: 我如何解决这个问题?
Spring MVC 3.2开始引入了基于Servlet 3的异步请求处理。相比以前,控制器方法已经不一定需要返回一个值,而是可以返回一个java.util.concurrent.Callable的对象,并通过Spring MVC所管理的线程来产生返回值。与此同时,Servlet容器的主线程则可以退出并释放其资源了,同时也允许容器去处理其他的请求。通过一个TaskExecutor,Spring M
我是webflux的新手,无法找到正确的材料来继续实现。 null
本文向大家介绍springmvc处理异步请求的示例,包括了springmvc处理异步请求的示例的使用技巧和注意事项,需要的朋友参考一下 springmvc 3.2开始就支持servlet3.0的异步请求。平常我们请求一个controller一般都是同步的,如果在代码执行中,遇到耗时的业务操作,那servlet容器线程就会被锁死,当有其他请求进来的时候就会受堵了。 springmvc3.2之后支持异
本文向大家介绍如果将axios异步请求同步化处理?相关面试题,主要包含被问及如果将axios异步请求同步化处理?时的应答技巧和注意事项,需要的朋友参考一下
在play,jersey,spring不同于具有池线程的典型多线程服务器。https://jersey.java.net/documentation/latest/async.html https://www.playframework.com/documentation/2.3.x/JavaAsync 一个线程正在侦听,并且在新线程中发生繁重的处理,此外,Web中的非阻塞请求不能与Java中的非