在ios中建立多个连接

时间:2013-06-13 11:30:07

标签: iphone ios objective-c

我在一个屏幕上使用2 UICollectionView并从我的api中获取2个不同的数据集。

但问题是我无法解决根据请求的视图来区分传入数据。

以下是我在UICollectionView中执行操作的代码段:

- (void)viewDidLoad
{

    [super viewDidLoad];

    NSURL *headlineurl = [NSURL URLWithString:@"api1"];
    headlinerequest = [NSURLRequest requestWithURL:headlineurl];
    [[NSURLConnection alloc] initWithRequest:headlinerequest delegate:self];

    NSURL *mostnewsurl = [NSURL URLWithString:@"api2"];
    NSURLRequest *mostnewsrequest = [NSURLRequest requestWithURL:mostnewsurl];
    [[NSURLConnection alloc] initWithRequest:mostnewsrequest delegate:self];

}

这是委托代码:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
   //What should I do here ?
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
   //What should I do here ?
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   //What should I do here ?
}

非常感谢。

1 个答案:

答案 0 :(得分:1)

当人们开始使用NSUrlConnection代表

时,您的问题非常常见

首先,你要将两个视图的委托设置为同一个对象,这可以起作用,但需要一些hackery。

我建议使用以下解决方案之一:

解决方案1(使用代表,更多工作)

创建一个新类,然后给它NSURlconnection delegate protocol 并称之为apiFetchDelegate

然后将您的委托方法放在那里-(void) connectionDidFinishLoading等等。

现在,在viewDidLoad方法中,将其更改为以下内容:

NSURL *headlineurl = [NSURL URLWithString:@"api1"];
headlinerequest = [NSURLRequest requestWithURL:headlineurl];

//Create a new instance of the delegate
apiFetchDelegate* headlineDelegate = [[apiFetchDelegate alloc] init];
[[NSURLConnection alloc] initWithRequest:headlinerequest delegate:headlineDelegate];

第二位代表:

NSURL *mostnewsurl = [NSURL URLWithString:@"api2"];
NSURLRequest *mostnewsrequest = [NSURLRequest requestWithURL:mostnewsurl];
//Create second delegate
apiFetchDelegate* mostnewsDelegate = [[apiFetchDelegate alloc] init];
[[NSURLConnection alloc] initWithRequest:mostnewsrequest delegate:mostnewsDelegate];

现在如您所见,每个人都会获得自己的委托,数据将不再混合!

解决方案2(没有代表,更少的工作要做)

对于你的需求,这可能是一个更好的解决方案,我不确定为什么你需要代表进行如此简单的调用,但如果你不这样做,最好采用这种简单的方法!。

我们将进行异步调用以避免在获取数据时冻结UI,这需要NSOperationQueue,以下是它的工作方式:

在viewDidLoad方法中,将代码更改为:

//Create your Queue here
NSOperationQueue *apiCallsQueue = [NSOperationQueue alloc] init];
[apiCallsQueue setMaxConcurrentOperations:2];

NSURL *headlineurl = [NSURL URLWithString:@"api1"];
headlinerequest = [NSURLRequest requestWithURL:headlineurl];

[NSURLConnection sendAsynchronousRequest:headlinerequest queue:apiCallsQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    //Here is your data for the first view
    //
    NSLog(@"Data for headline view: %@", [[NSString alloc] initWithData:data
                                       encoding:NSUTF8StringEncoding]);
}];

对于第二种观点:

NSURL *mostnewsurl = [NSURL URLWithString:@"api2"];
NSURLRequest *mostnewsrequest = [NSURLRequest requestWithURL:mostnewsurl];
[NSURLConnection sendAsynchronousRequest:mostnewsrequest queue:apiCallsQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    //Here is your data, for the second view
    //
    NSLog(@"Date for latest news: %@", [[NSString alloc] initWithData:data
                                       encoding:NSUTF8StringEncoding]);
}];

请告诉我这是否适合您,或者您是否需要进一步的帮助。