首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用sendAsynchronousRequest:queue:completionHandler:

如何使用sendAsynchronousRequest:queue:completionHandler:
EN

Stack Overflow用户
提问于 2012-02-14 09:17:06
回答 6查看 85.3K关注 0票数 74

两部分问题

第一部分:我正在尝试创建一个对我的数据库的ASynchronous请求。我目前正在同步进行,但我想同时学习这两种方法,以更好地理解正在发生的事情。

目前我已经像这样设置了我的同步调用。

代码语言:javascript
复制
- (IBAction)setRequestString:(NSString *)string
{
    //Set database address
    NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"http://127.0.0.1:8778/instacodeData/"]; // imac development

    //PHP file name is being set from the parent view
    [databaseURL appendString:string];

    //call ASIHTTP delegates (Used to connect to database)
    NSURL *url = [NSURL URLWithString:databaseURL];

    //SynchronousRequest to grab the data
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSError *error;
    NSURLResponse *response;

    NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (!result) {
        //Display error message here
        NSLog(@"Error");
    } else {

        //TODO: set up stuff that needs to work on the data here.
        NSString* newStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
        NSLog(@"%@", newStr);
    }
}

我想我需要做的就是把电话

代码语言:javascript
复制
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

使用ASynchronous版本

代码语言:javascript
复制
sendAsynchronousRequest:queue:completionHandler:

然而,我不确定要传递给队列或completionHandler的是什么……任何示例/解决方案都将不胜感激。

第二部分:我一直在读关于多任务处理的文章,我想通过确保我的连接请求在中断时完成来支持它。我一直在关注这个example

它解释了如果中断发生,如何获得更多的时间,我知道它在做什么。但不知道如何将其应用于此连接?如果您有任何示例/教程来帮助我弄清楚如何应用它,那将是非常棒的!

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2012-02-14 09:58:24

PArt 1:

代码语言:javascript
复制
NSURL *url = [NSURL URLWithString:urlString];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
    if ([data length] > 0 && error == nil)
        [delegate receivedData:data];
    else if ([data length] == 0 && error == nil)
        [delegate emptyReply];
    else if (error != nil && error.code == ERROR_CODE_TIMEOUT)
        [delegate timedOut];
    else if (error != nil)
        [delegate downloadError:error];
}];
票数 114
EN

Stack Overflow用户

发布于 2012-05-02 23:45:07

下面是一个示例:

代码语言:javascript
复制
NSString *urlAsString = @"http://www.cnn.com";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];


[NSURLConnection
         sendAsynchronousRequest:urlRequest
         queue:[[NSOperationQueue alloc] init]
         completionHandler:^(NSURLResponse *response,
                             NSData *data,
                             NSError *error) 
        {

         if ([data length] >0 && error == nil)
         {

                        // DO YOUR WORK HERE

         }
         else if ([data length] == 0 && error == nil)
         {
             NSLog(@"Nothing was downloaded.");
         }
         else if (error != nil){
             NSLog(@"Error = %@", error);
         }

     }];
票数 38
EN

Stack Overflow用户

发布于 2013-06-26 05:42:36

对于队列参数,试试这个魔术:

代码语言:javascript
复制
[NSOperationQueue mainQueue]

如果您要在请求完成时更新UI,这将非常有效,因为主队列是主线程。它本质上为您提供了NSURLConnection之前的行为。但是,如果您计划写入文件或解压缩,那么您可以在后台队列上完成,然后将异步调度回主队列以进行UI更新。

票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9270447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档