DarkLeafyGreen
DarkLeafyGreen

Reputation: 70426

Make requests in loop using restkit

In a UITableViewController I try to do this:

In interface:

@property (nonatomic, strong) NSMutableArray *tickets;

In implementation

for(NSString *t in [SharedPreferences getTickets]){

    [[TicketListManager sharedManager] ticket:^(Ticket *sc) {

       [self.tickets addObject:sc];
       [self.tableView reloadData];

        NSLog(@"ticket %d", self.tickets.count);
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"error");
    } ticket:ticketId];
}

While all objects are loaded successfully (I logged them), the count of tickets is always 0, so my table wont update. Any ideas why the objects are not saved to the array? I am on xcode 5, iOS7 and restkit 0.20.3

Upvotes: 0

Views: 61

Answers (2)

Wain
Wain

Reputation: 119031

Most likely self.tickets is not initialised. Set:

self.tickets = [NSMutableArray array];

When you create the controller.

Aside: your method ticket:failure:ticket: isn't very clear and would be better as getTicketWithId:success:failure:.

Another aside: be careful with starting network connections in a loop as you could flood the network. You may want to limit the concurrent connection by editing the queue of the http client.

Upvotes: 3

Viper
Viper

Reputation: 1408

Try this may be this work -

for(NSString *t in [SharedPreferences getTickets]){

        [[TicketListManager sharedManager] ticket:^(Ticket *sc) {

            [self.tickets addObject:sc];
            [self performSelectorOnMainThread:@selector(reloadTableData) withObject:Nil waitUntilDone:TRUE];

            NSLog(@"ticket %d", self.tickets.count);
        } failure:^(RKObjectRequestOperation *operation, NSError *error) {
            NSLog(@"error");
        } ticket:ticketId];
    }

-(void)reloadTableData{
    [self.tableView reloadData];
}

Upvotes: 0

Related Questions