【问题标题】:Using NSURLConnection sendAsynchronousRequest使用 NSURLConnection 发送异步请求
【发布时间】:2013-02-12 16:29:46
【问题描述】:

我在 Xcode 4.5.2 中有一个应用程序。它发送一个 URL 请求以下载图像并将图像设置在图像视图中。我有以下代码可以正常工作:

dispatch_queue_t downloadQueue = dispatch_queue_create("Get Facebook Friend", NULL);
    dispatch_async(downloadQueue, ^{

self.firstFriendImage = [UIImage imageWithData:
                           [NSData dataWithContentsOfURL:
                            [NSURL URLWithString:
                             [[self.facebookPhotosAll objectAtIndex:self.randomIndex1]
                              objectForKey:@"pic_big"]]]];

dispatch_async(dispatch_get_main_queue(), ^{
            [self postDownloadTasks:self.topView setLabel:self.firstFriendLabel 
withFriendName:self.firstFriendName cropImage:self.firstFriendImage 
inImageView:self.friendOneView atYPoint:22];
  });
      });

因此,尽管这段代码运行良好,但对于 Objective C 来说是新手,但我正在尝试稍微探索一下该语言,看看我还能如何做同样的事情。所以我尝试使用 NSURLConnection sendAsynchronousRequest: 方法(在查看了这里的一些示例之后),但我似乎无法让这个方法工作。这就是我所做的:

 NSOperationQueue *queue = [[NSOperationQueue alloc]init];
NSString *string = [[self.facebookPhotosAll  
                   objectAtIndex:self.randomIndex1]objectForKey:@"pic_big"];

    [NSURLConnection sendAsynchronousRequest: [NSURL URLWithString:   
           string] queue:queue   
             completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
        // Make sure eveything is ok
        if(error){
            // do something
        }

        self.firstFriendImage = [UIImage imageWithData:data];
         dispatch_async(dispatch_get_main_queue(), ^{

            [self postDownloadTasks:self.topView setLabel:self.firstFriendLabel 
withFriendName:self.firstFriendName cropImage:self.firstFriendImage  
inImageView:self.friendOneView atYPoint:22];

         });

    }];

所以这段代码根本不起作用(它没有返回任何数据)。调用该方法时应用程序崩溃,我收到以下消息:

** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSURL _CFURLRequest]:无法识别的选择器已发送到实例 0xa39dd20”

谁能告诉我如何使用 NSURLConnection sendAsynchronousRequest 方法完成我在第一个代码摘录中所做的事情?

【问题讨论】:

  • NSError 对象是什么意思?在你的 if 语句中只是一个赞扬。你用 NSLog() 做过测试吗?
  • 我不认为这是你的问题,但它是在你提供的队列上运行的完成块(不是请求)——如果你使用 [NSOperationQueue mainQueue],那么你不要不需要 dispatch_async 里面的东西。
  • 它甚至不足以产生错误,只要调用该方法,程序就会崩溃,并显示以下内容:-[__NSDictionaryI length]: unrecognized selector sent to instance 0xa2da000...由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSDictionaryI 长度]:无法识别的选择器发送到实例 0xa2da000”
  • @user1697845,你确定 self.facebookPhotosAll 是一个数组吗?我认为该错误与 sendAsynchronousRequest 无关:调用自身
  • 您是否在 Breakpoint Navigatior 中添加了 All Exeptions 断点?

标签: objective-c asynchronous nsurlrequest


【解决方案1】:

在发送异步请求方法中,您的 URL 字符串缺少一部分。它应该就像你的第一个方法一样:

[[self.facebookPhotosAll objectAtIndex:self.randomIndex1] objectForKey:@"pic_big"]

【讨论】:

  • 谢谢我刚刚看到并修复了它,但它仍然不起作用!现在我在崩溃时收到以下错误:** 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[NSURL _CFURLRequest]: unrecognized selector sent to instance 0xa39dd20'
  • @user1697845,我也错过了你代码中的那个错误——你需要创建一个 NSURLRequest。您只是提供一个 URL 而不是 URLRequest 作为 sendAsync 的第一个参数....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-16
相关资源
最近更新 更多