gpt4 book ai didi

iphone - NSURLConnection 和中央调度

转载 作者:行者123 更新时间:2023-12-03 18:14:38 24 4
gpt4 key购买 nike

是否建议将 NSUrlConnection 包装在 gcd 样式 block 中并在 low_priority 队列上运行它?

我需要确保我的连接不会发生在主线程上,并且连接需要异步。我还需要同时发出多个请求。

如果我采用 gcd 路线,我不确定 NSUrlConnectionDelegate 方法会在哪个线程上调用。

NSURLConnection 依赖于委托(delegate),因此一旦连接完成,无论处理它的包装类都需要调用其调用者。我不确定如何处理连接工作启动/完成时调用的所有各种回调:

- (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 block 中吗?如果我想取消通话,请使用“dispatch_suspend”?

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

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

最佳答案

我推荐你看WWDC Sessions关于iPhone OS中的网络应用。

  • WWDC 2010 第 207 场 session - 适用于 iPhone 操作系统的网络应用程序,第 1 部分
  • WWDC 2010 第 208 场 session - 适用于 iPhone 操作系统的网络应用程序,第 2 部分

讲师说

"Threads Are Evil™"

对于网络编程,推荐使用带有RunLoop的异步网络编程。使用后台线程(Grand Central Dispatch Concurrent Queue)进行线程安全的数据处理,而不是进行网络编程。

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

  • WWDC 2010 第 206 场 session - 介绍 iPhone 上的 Blocks 和 Grand Central Dispatch
  • WWDC 2010 第 211 场 session - 使用 Grand Central Dispatch 简化 iPhone 应用程序开发

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

<小时/>

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

#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类。

/*
* 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! */

}];

关于iphone - NSURLConnection 和中央调度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5037545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com