casillas
casillas

Reputation: 16813

CollectionView Delegates called before URL Connection Finishes

I am implementing a CollectionView with URL Connection.

The problem that I have is as follows the collectionView delegates are called before the URL connection is finished. Therefore, Collection View does not get any data to display or how could I reload collection view.

Here is my implementation.

-(id)init{
    self = [super init];
    if (self){
        [self loadFromURL];
    }
    return self;
}

-(void)loadFromURL{

    NSURLRequest *theRequest = [NSURLRequest requestWithURL:
                                [NSURL URLWithString:@"http://www.appybyte.com/satgitsin/products.php?zip=77072"]];

    NSURLConnection *theConnection=[[NSURLConnection alloc]
                                    initWithRequest:theRequest delegate:self];
    if(theConnection){
        responseData = [[NSMutableData alloc] init];
    } else {
        NSLog(@"failed");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [responseData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSString *msg = [NSString stringWithFormat:@"Failed: %@", [error description]];
    NSLog(@"%@",msg);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError *myError = nil;
    NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves  error:&myError];

    self.pElements = [[NSMutableArray alloc] init];
    for (NSDictionary *aModuleDict in res){
        PMosaicData *aMosaicModule = [[PMosaicData alloc] initWithDictionary:aModuleDict];
        [self.pElements addObject:aMosaicModule];
    }

}

#pragma mark - UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.pElements count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cell";
    PMosaicCell *pCell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    PMosaicData *pData=[self.pElements objectAtIndex:indexPath.row];
    pCell.pMosaicData = pData;
    return pCell;
}

By the way, my collectionView is defined in another class called CustomViewController.h as __weak IBOutlet UICollectionView *_collectionView; How could I able to make it work?

Upvotes: 1

Views: 74

Answers (1)

Casey
Casey

Reputation: 6701

at the end of your connectionDidFinishLoading: call [self.collectionView reloadData].

I would recommend using NSURLConnection's class method sendAsynchronousRequest:queue:options:error: instead of of the delegate pattern since you aren't doing any custom buffering logic.

The final code should look something like this:

-(void)loadFromURL {
    NSURL *url = [NSURL URLWithString:@"http://www.appybyte.com/satgitsin/products.php?zip=77072"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    __weak typeof(self) weakSelf = self;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        NSError *myError = nil;
        NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];

        // probably should handle the error here
        if (myError) {

        }

        weakSelf.pElements = [[NSMutableArray alloc] init];
        for (NSDictionary *aModuleDict in res){
            PMosaicData *aMosaicModule = [[PMosaicData alloc] initWithDictionary:aModuleDict];
            [weakSelf.pElements addObject:aMosaicModule];
        }

        [weakSelf.collectionView reloadData];
    }];
}

Upvotes: 2

Related Questions