2013-06-13 45 views
-2

我在一個屏幕上使用了2 UICollectionView,並從我的api中獲取了2個不同的數據集。在ios中建立多個連接

但問題是,我無法解決的區別傳入的數據取決於所請求的視圖。

下面是我在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

我想從collectionviews中的不同url中提取數據 – Sezgin

+0

嗯,我看到你可能將所有人都設置爲同一個代表,這將最終導致數據進入,你不知道它來自哪裏,我會現在給你一個關於如何去做的小例子。 –

回答

1

你的問題是很常見的,當人們開始使用NSUrlConnection代表,

第一件事,那你就是在兩種觀點的同一個對象,它可以工作,但將需要一些兩輪牛車的委託。

我推薦了以下解決方案之一:

解決方案1(使用委託,更多的工作)

創建一個新的類,並賦予給它的NSURlconnectiondelegate 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]); 
}]; 

讓我知道這對你的作品,或者如果您需要進一步的援助。