如何在目标c中加载数据异步浏览滚动和核心数据获取



我的应用中具有核心数据库。在该数据基础中,一个模型课程中有250000个记录,每个记录都有大约5个字段。

每个记录一个uiateveviewcell的值。

,但我不想在单个提取请求中获取所有记录。想要根据UITATION VIEW卷轴获取一些n n次记录。

示例:

当Table View Load First Tires想要获取20个记录时,每当用户滚动Uaithitview需要获取下一个20个记录,就像需要基于uitableview的滚动来获取模型中的数据。

我该如何实现。请帮助.....

尝试以下:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == arrObject.count-1) {
        if (arrObject.count  < totalDataCount) {
            currentPage++; // initial value is zero
            [self getDataFromResource]; // call api here
        }
    }
}
//Try this:-

//使用谓词您可以根据滚动从核心数据中获取20个记录。

     -(void)viewDidLoad 
        {
           [super viewDidLoad];
         UIRefreshControl *refreshControl;
            [refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];
            minId = 0;
            maxId = 20;
            maxId = minId +maxId;
            NSPredicate*preicate = [NSPredicate predicateWithFormat:@"id == %d && id < %d",minId,maxId];// id is your unique id in core data.
          //  Fetch first 20 records from core data using predicate.
        }
        - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
        {
            float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
            if (bottomEdge >= self.tableView.contentSize.height) //this will get last record of tableview when scroll
                {
                    minId = maxId;
                    maxId = minId + maxId;
                    NSPredicate*preicate = [NSPredicate predicateWithFormat:@"id == %d && id < %d",minId,maxId];// id is your unique id in core data.
                    //  Fetch next 20 records from core data using predicate.
    [self.tableView reloadData];
                }
        }
   // Whenever scroll down ,refresh method is called
    - (void)refreshTable {
      [self.yourArray removeAllobjects];
    minId = 0;
            maxId = 20;
            maxId = minId +maxId;
            NSPredicate*preicate = [NSPredicate predicateWithFormat:@"id == %d && id < %d",minId,maxId];// id is your unique id in core data.
          //  Fetch first 20 records from core data using predicate.
    [self.tableView reloadData]
    }

@satya murty 尝试这个:- 使用Core Data异步进行异步。

    - (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            static NSString *CellIdentifier = @"Cell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
            cell.tag = indexPath.row;
            //UserRecords TableName
            //self.yourArray records array (fetch from core data)
            UserRecords*records = self.yourArray[indexPath.row];
            if (records)
            {
                cell.imageView.image = nil;
                dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
                dispatch_async(queue, ^(void) {
                    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:records.imageURL];
                                         UIImage* image = [[UIImage alloc] initWithData:imageData];
                                         if (image) {
                                             dispatch_async(dispatch_get_main_queue(), ^{
                                                 if (cell.tag == indexPath.row) {
                                                     cell.imageView.image = image;
                                                     [cell setNeedsLayout];
                                                 }
                                             });
                                         }
                });
                cell.textLabel.text = records.title;
            }
return cell;
        }

最新更新