Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using 2 UICollectionView on one screen and fetching 2 different data sets from my api.

But the problem is that I can't resolve is differentiating the incoming data depending on which view requested.

Here's a code snippet of what I do in the 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];

}

And this is the code from the delegate:

- (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 ?
}

Thanks a lot.

share|improve this question
1  
Please give more details about what you want to accomplish, this is not clear. –  Mostafa Torbjørn Berg Jun 13 '13 at 11:33
 
I want pull data from different url in collectionviews –  Sezgin Jun 13 '13 at 11:35
 
Well I see you're probably setting all of them to the same delegate, which will end up in data coming in and you'll have no idea where it's coming from, I'll give you a little example on how to do it now. –  Mostafa Torbjørn Berg Jun 13 '13 at 21:17
add comment

1 Answer

up vote 0 down vote accepted

Your problem is pretty common when people start using NSUrlConnection delegates,

First thing you're setting the delegate of both views to the same object, which can work, but will need some hackery.

I recommend one of the following solutions:

Solution 1 (uses delegates, more work)

Create a new class, and give give it the NSURlconnection delegate protocol and call it something like apiFetchDelegate

Then place your delegate methods in there -(void) connectionDidFinishLoading, etc..

Now in your viewDidLoad method, change it to the following:

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];

And the second delegate:

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

now as you see, each one will get its own delegate, and data won't be mixed anymore !

Solution 2 (without delegates, less work to do)

This is probably a better solution for your need , I'm not sure why you need delegates for such a simple call, but if you don't , better go with this simple way !.

We will make async calls to avoid freezing the UI while data is being fetched, this will require an NSOperationQueue, here's how it'll work:

In your viewDidLoad method, change the code to this:

//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]);
}];

And for the second view:

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]);
}];

Let me know if this works for you or if you need further assistance.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.