ios - UITableView - AFNetworking 后台操作卡住滚动

标签 ios objective-c uitableview asynchronous afnetworking

我有一个带有 UITableViewController 的示例应用程序。

与 facebook 新闻源一样,该应用程序应该下载第一次 X 新闻,然后随着用户滚动逐渐获取新闻。

这是我的实现:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

if (indexPath.row == self.newsList.count-PADDLE_BEFORE_FETCHING && !cantFetchMore)
    if (!fetching){
        fetching = YES;

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
            [self fetchNews];
        });


    }

}

(我们的想法是,当我们到达 N-PADDLE_BEFORE_FETCHING 单元时,仅当我们仍然可以获取一些新闻时才开始获取其他新闻 - 见下文 - 并且当前尚未运行获取)

然后执行 fetchNews :

-(void)fetchNews{


    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *url = [NSString stringWithFormat:@"%@%@%@%@%d%@",HOSTNAME,GET_NEWS,[defaults objectForKey:@"oAuthToken"],@"&limit=",FETCH_SIZE_NEWS,[NSString stringWithFormat:@"&offset=%d",self.newsList.count]];

    NSURLRequest *request =[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]];


    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

        #if DEVELOPMENT_MODE
                NSLog(@"News : %@",JSON);
                NSLog(@"Response : %@\n Request : %@",response,request);
        #endif

        //NSLog(@"Number of news fetched : %d",((NSArray*)JSON[@"data"]).count);

        for (NSDictionary *d in JSON[@"data"]){
            News *new = [[News alloc] initWithDictionary:d];
            [self.newsList addObject:new];
            new = nil;
        }


        if ((((NSArray*)JSON[@"data"]).count)%FETCH_SIZE_NEWS !=0) cantFetchMore = YES;
        //NSLog(@"%d cantFetch",cantFetchMore);

        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

        [self.tableView reloadData];

        fetching = NO;

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request error : %@ %@ %@",request,error, JSON);
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        fetching = NO;

    }];


    [operation start];

}

这将从服务器获取 FETCH_SIZE_NEWS 附加新闻,从正确的偏移量开始,该偏移量是 newsList 数组的当前大小。 另外,如果获取的新闻计数 % FETCH_SIZE_NEWS 不等于 0,则意味着我们无法获取其他新闻(这将阻止在滚动 UITableView 时调用 Web 服务)。

我的问题是,当提取完成时(正是当我看到状态栏中运行的事件轮时),它会阻止 GUI,并且我无法继续从 n-PADDLE_BEFORE_FETCHING 单元格向下滚动到 n 单元格,甚至向上滚动到之前加载的单元格。

我真的不明白为什么 AFNetworking 应该异步运行。

有什么想法吗?

谢谢

最佳答案

完成 block 中的 for 循环正在主线程上运行,可能会导致速度变慢。尝试将该代码发送到另一个线程/队列。

关于ios - UITableView - AFNetworking 后台操作卡住滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18962184/

相关文章:

ios - IOS如何获取文件下载的百分比

iOS 应用间通信

iphone - TableCell 中的 UITextView,如何为通用应用程序设置正确的宽度

ios - UITableViewCell Z 顺序

ios - 无法在 Storyboard 中将 UIImageView 放在 UITableView 后面

ios - 更改 UISegmentedControl 中特定段的标题文本颜色?

IOS:向后移动两个 View

objective-c - 使用自定义文件格式的 registerForDraggedTypes

ios - UISlider 平滑改变值

ios - Objective-C - 有没有办法将 UIView 属性作为数组?