下面的代码与我在 ASI 中使用的代码几乎相同,但现在我使用的是 AFNetworking。我的猜测是它很慢,因为它在主线程上运行成功块。我尝试将 successCallbackQueue 设置为新队列,但它似乎不起作用。它只是非常慢并且不能有效地做到这一点。如何加快速度或确保它在后台线程中运行?
#define kPerPage 10
- (void) pullData {
NSURL *url = [API homeRecentUrlWithPage:self.currentRecentPage limit:kPerPage];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
dispatch_queue_t requestQueue = dispatch_queue_create("requestQueue", NULL);
AFJSONRequestOperation *operation;
operation.successCallbackQueue = requestQueue;
operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSArray* modelArray = [JSON objectForKey:@"models"];
for (int i = 0; i < [modelArray count]; i++)
{
Model *b = [Model alloc];
b = [b initWithDict:[Model objectAtIndex:i]];
[self.otherArray addObject:b];
}
[_modelTable reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"%@", [error userInfo]);
}];
[operation start];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* identifier = @"ModelTableCell";
cell = (ModelTableCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[ModelTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell.selectionStyle = UITableViewCellAccessoryNone;
}
if([indexPath row] == (self.currentRecentPage-1) * kPerPage + 5) {
NSLog(@"%d aaa", self.currentRecentPage);
self.currentRecentPage++;
[self pullData];
}
Model *b = [self.models objectAtIndex:[indexPath row]];
[cell populateWithModel:b];
return cell;
}