NSURLConnection和盛大的中央调度 [英] NSURLConnection and grand central dispatch

查看:97
本文介绍了NSURLConnection和盛大的中央调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

建议将NSUrlConnection包装在gcd样式块中并在low_priority队列上运行吗?

Is it advisable to wrap up NSUrlConnection in a gcd style blocks and run it on a low_priority queue?

我需要确保我的连接没有发生在主线程和连接需要是异步的。我还需要同时发出几个请求。

I need to ensure that my connections are not happening on the main thread and the connections need to be asynchronous. I also need several simultaneous requests to go at once.

如果我去gcd路由,我不确定调用NSUrlConnectionDelegate方法的哪个线程。

If I go the gcd route, I'm not sure which thread the NSUrlConnectionDelegate methods get invoked on.

NSURLConnection依赖于委托,所以一旦连接完成,处理它的任何包装类都需要调用它的调用者。我不确定如何处理连接工作启动/结束时调用的所有各种回调:

NSURLConnection relies on delegates so once the connection is complete, whatever wrapper class that handles it will need to invoke its caller. I'm not certain how to deal with all of the various callbacks that are invoked when the connection work starts up/finishes:

- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)incrementalData;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

我应该只调用同步版本但是用gcd块包装吗?如果我要取消呼叫,请使用'dispatch_suspend'?

Should I just call the synchronous versions but wrapped in gcd blocks? And if I want to cancel a call, use 'dispatch_suspend'?

dispatch_async(queue,^{
      NSString* result = [self mySynchronousHttp:someURLToInvoke];
      });

// If I need to cancel
dispatch_suspend(queue);


推荐答案

我建议您查看有关网络应用程序的WWDC会话iPhone OS。

I recommend you to see WWDC Sessions about network application in iPhone OS.


  • WWDC 2010 Session 207 - 适用于iPhone OS的网络应用程序,第1部分

  • WWDC 2010年会议208 - 适用于iPhone OS的网络应用程序,第2部分

讲师说


Threads Are Evil™

"Threads Are Evil™"

用于网络编程,并建议使用异步网络编程RunLoop。使用后台线程(Grand Central Dispatch Concurrent Queue)进行线程安全数据处理,而不是网络编程。

for network programming and recommended to use asynchronous network programming with RunLoop. Use background thread (Grand Central Dispatch Concurrent Queue) for thread-safe data processing, not for network programming.

顺便说一句,Blocks和Grand Central Dispatch会话也值得看看。

By the way, Blocks and Grand Central Dispatch sessions are also worth to see.


  • WWDC 2010会议206 - 在iPhone上介绍块和Grand Central发送消息

  • WWDC 2010年会议211 - 通过Grand Central Dispatch简化iPhone应用程序开发

我为异步NSURLConnection编写了一个包装类。

I wrote a wrapper class for asynchronous NSURLConnection.

AsyncURLConnection.h

AsyncURLConnection.h

#import <Foundation/Foundation.h>

typedef void (^completeBlock_t)(NSData *data);
typedef void (^errorBlock_t)(NSError *error);

@interface AsyncURLConnection : NSObject
{
    NSMutableData *data_;
    completeBlock_t completeBlock_;
    errorBlock_t errorBlock_;
}

+ (id)request:(NSString *)requestUrl completeBlock:(completeBlock_t)completeBlock errorBlock:(errorBlock_t)errorBlock;
- (id)initWithRequest:(NSString *)requestUrl completeBlock:(completeBlock_t)completeBlock errorBlock:(errorBlock_t)errorBlock;
@end






AsyncURLConnection.m


AsyncURLConnection.m

#import "AsyncURLConnection.h"

@implementation AsyncURLConnection

+ (id)request:(NSString *)requestUrl completeBlock:(completeBlock_t)completeBlock errorBlock:(errorBlock_t)errorBlock
{
    return [[[self alloc] initWithRequest:requestUrl
        completeBlock:completeBlock errorBlock:errorBlock] autorelease];
}

- (id)initWithRequest:(NSString *)requestUrl completeBlock:(completeBlock_t)completeBlock errorBlock:(errorBlock_t)errorBlock
{

    if ((self=[super init])) {
        data_ = [[NSMutableData alloc] init];

        completeBlock_ = [completeBlock copy];
        errorBlock_ = [errorBlock copy];

        NSURL *url = [NSURL URLWithString:requestUrl];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [NSURLConnection connectionWithRequest:request delegate:self];
    }

    return self;
}

- (void)dealloc
{
    [data_ release];

    [completeBlock_ release];
    [errorBlock_ release];
    [super dealloc];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [data_ setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [data_ appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    completeBlock_(data_);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    errorBlock_(error);
}

@end






如何使用AsyncURLConnection类。


How to use AsyncURLConnection class.

/*
 * AsyncURLConnection -request:completeBlock:errorBlock: have to be called
 * from Main Thread because it is required to use asynchronous API with RunLoop.
 */

[AsyncURLConnection request:url completeBlock:^(NSData *data) {

    /* success! */

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        /* process downloaded data in Concurrent Queue */

        dispatch_async(dispatch_get_main_queue(), ^{

            /* update UI on Main Thread */

        });
    });

} errorBlock:^(NSError *error) {

    /* error! */

}];

这篇关于NSURLConnection和盛大的中央调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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