0

我有一个自定义 UITableViewController 类,它在我的应用程序的几个 ViewController 之间共享。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ... 
    // Check if we are almost at the end of the scroll. If so, start fetching.
    // Doing this here is better than overriding scrollViewDidEndDragging
    if (indexPath.row >= [self.tableView numberOfRowsInSection:0] - 3) {
        [self refill];
    }

    return cell;
}

我想在用户到达页面底部之前进行抢先获取。

但是,我注意到如果用户向上滚动然后再次向下滚动,我的 VC 将触发另一个获取。

所以我想出了一个标志 isFetching 来检查以防止重复获取。不幸的是,我正在运行异步代码,我需要一种方法来在我的获取操作完成后重置这个标志。

饲料VC:

- (void)refill {
    if (self.continueFetching && !self.isFetching) {
        NSLog(@"Fetching started");
        self.isFetching = true;
        AFJSONRequestOperation *operation = [self.delegate refill:^{
            NSLog(@"Fetching completed");
            self.isFetching = false;
        }];
        [self.pendingOperations addObject:operation];
    }
}

代表:

- (AFJSONRequestOperation *)refill:(void (^)(void))finishCallback {
    return [[ItemStore sharedInstance] fetchItemsForFeed:^(int itemsFetched, int nextOffset, bool hasNextPage) {
        finishCallback();
    } withFailureBlock:^{
        finishCallback();
    }];
}

我发现为了翻转布尔值而绕过一个完成块非常痛苦。有人有更好的推荐吗?

4

1 回答 1

0

阐述:

typedef void(^block_t)(void);
typedef void(^success_block_t)(int itemsFetched, int nextOffset, bool hasNextPage);

饲料CV:

/****************************************************************************/
/*!
 @note assume the declarations:
 @property (nonatomic,strong) NSOperation* fetchOp;
 @property (nonatomic,strong) NSOperationQueue* operationQueue;
 */
/****************************************************************************/
- (void) refill
{
    if (self.fetchOp) { //fetch is in progress
        return;
    }

    __block __weak id weakSelf = self;
    block_t completionBlock = ^{ //this will happen on a BG thread
        //Implement what ever logic seems to fit your FeedVC uppon completion
        //This will be called on the operation thread, so don't update UI here
        //Move to main thread UI updates
        [weakSelf setFetchOp:nil];
    };

    self.fetchOp = [self.delegate refill:completionBlock];
//                            successBlock:nil
//                               failBlock:nil];

    //If you can't change your delegate code then:
    //self.fetchOp = [self.delegate refill:nil];
    //[self.fetchOp setCompletionBlock:completionBlock];

    [self.operationQueue addOperation:self.fetchOp];
}
/****************************************************************************/

代表:

/****************************************************************************/
/*!
 @note If you can change this API add support for the blocks
 */
/****************************************************************************/
- (AFJSONRequestOperation*) refill:(block_t)completionBlock
//                      successBlock:(success_block_t)success
//                         failBlock:(block_t)fail
{
    AFJSONRequestOperation* op = [[ItemStore sharedInstance] fetchItemsForFeed:nil/*success*/
                                                              withFailureBlock:nil/*fail*/];
    [op setCompletionBlock:completionBlock];
    return op;
}
/****************************************************************************/
于 2013-04-06T10:19:56.527 回答