to many requests in objective-c, please help

Discussion in 'iPhone/iPad Programming' started by wfs123, Feb 3, 2013.

  1. macrumors newbie

    Joined:
    Mar 28, 2011
    #1
    Hi there, I'm developing that is grabbing jpegs from parse then placing them inside of a tableview. But whenever I scroll through the table view it lags. I have figured out that this lagging is because I'm sending to many requests.

    What I would like to do is send one request when the app launches, download the images then place them in the UITableView. Here is the code where the problem is occurring:

    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
        static NSString *CellIdentifier = @"cell";
        
        feedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[feedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        
        // Add the user's name
        cell.userName.text = [object objectForKey:@"name"];
        // Add the amount of likes
        cell.likes.text = [ NSString stringWithFormat:@"%@", [object objectForKey:@"Likes"]];
        
    
        PFFile *thumbnail = [object objectForKey:@"imageFile"];
        
        PFImageView *pfimg = [[PFImageView alloc] initWithFrame:CGRectMake(30, 27, 260, 260)];
     
    //   
    //this is where the problem is happening:
    //
    
    [B]    [thumbnail getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
            if (!error) {
                UIImage *image = [UIImage imageWithData:data];
                pfimg.image = image;
                [pfimg loadInBackground];
                NSLog(@"No Error");
            }else{
                NSLog(@"Error: %@",error);
            }
        }];
    [/B]    
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell addSubview:pfimg];
        return cell;
    }
    thank you so much!

    ~Will
     
  2. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    Apple Sample Code LazyTableImages.
     
  3. macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #3
    Also checkout NSOperationQueues. They allow you to limit the number of concurrent operations (Downloads).
     

Share This Page