良好的互联网请求模式与大中央调度? [英] Good pattern for Internet requests with Grand Central Dispatch?

查看:165
本文介绍了良好的互联网请求模式与大中央调度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用具有GCD队列的同步ASIHTTPRequest从Internet下载数据,然后使用JSONKit解析响应数据。你对这种模式有什么看法。提前感谢。



这是我的代码:

  dispatch_async (队列,^(void){

//请求是ASIHTTPRequest。
[request startSynchronous];

//解析JSON
NSArray * array = [[request responseData] objectFromJSONDataWithParseOptions:JKParseOptionLooseUnicode];

//调用主队列来更新UI
dispatch_async(dispatch_get_main_queue(),$(void){
callbackBlock(array);
});
});

编辑:我使用ASIHTTPRequest的原因是我需要修改OAuth的请求头,

解决方案

因此您替换了

   - (void)doDownload {
NSURL * url = [NSURL URLWithString:@http://foobar.com];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
connection = [[NSURLConnection alloc] initWithRequest:aURLRequest delegate:self];
receivedData = [[NSMutableData data] retain];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSArray * array = [_receivedData objectFromJSONDataWithParseOptions:JKParseOptionLooseUnicode];
callbackBlock(array);
}

pre> - (void)doDownload {
NSURL * url = [NSURL URLWithString:@http://foobar.com];
ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:url];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

dispatch_async(queue,^(void){
[request startSynchronous];
NSArray * array = [[request responseData] objectFromJSONDataWithParseOptions:JKParseOptionLooseUnicode];

//调用主队列来更新UI
dispatch_async(dispatch_get_main_queue(),^(void){
callbackBlock(array);
});
} ;
}

来自ASIHTTPRequest的10,000行代码



它是什么?



NSURLConnection是完全异步的,使用GCD,缓存,自动压缩/解压缩,等等。



因为这个原因,单单在你提供的(可能不完整)信息,我会说,这是一个非常可怕的一块代码。



当然,上下文是一切 - 你可能有一个真正的,真正的真正重要的原因来重新实现已经存在的由Apple提供的库代码。 / p>

I'm currently using synchronous ASIHTTPRequest with GCD queues to download data from the Internet, then parse the response data with JSONKit. What do you think about this pattern. Thank you in advance.

Here is my code:

    dispatch_async(queue, ^(void) {

        // Request is ASIHTTPRequest.
        [request startSynchronous];

        // Parse JSON.
        NSArray *array = [[request responseData] objectFromJSONDataWithParseOptions:JKParseOptionLooseUnicode];

        // Callback on the main queue to update UI.
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            callbackBlock(array);
        });
    });

EDIT: The reason I use ASIHTTPRequest is that I need to modify the request header for OAuth and use POST method to upload images.

解决方案

So you replaced this

- (void)doDownload {
    NSURL *url = [NSURL URLWithString:@"http://foobar.com"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    connection = [[NSURLConnection alloc] initWithRequest:aURLRequest delegate:self];
    receivedData = [[NSMutableData data] retain];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [_receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSArray *array = [_receivedData objectFromJSONDataWithParseOptions:JKParseOptionLooseUnicode];
    callbackBlock(array);       
}

with this -

- (void)doDownload {
    NSURL *url = [NSURL URLWithString:@"http://foobar.com"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(queue, ^(void) {
        [request startSynchronous];
        NSArray *array = [[request responseData] objectFromJSONDataWithParseOptions:JKParseOptionLooseUnicode];

        // Callback on the main queue to update UI.
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            callbackBlock(array);
        });
    });
}

and 10,000+ lines of code from ASIHTTPRequest.

What has it got you?

NSURLConnection is fully asynchronous, uses GCD, caches, automatic zip/unzip, etc, etc..

For that reason, and going solely on the (possibly incomplete) information you provided, i'd say that it was a really awful piece of code.

Of course, context is everything - and you may have a really, really, really good reason for reimplementing the already existing functionality of Library code provided by Apple.

这篇关于良好的互联网请求模式与大中央调度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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