【问题标题】:How to load data asynchronously UITableview scrolling and core data fetch in objective c如何在目标c中异步加载数据UITableview滚动和核心数据获取
【发布时间】:2017-09-28 19:08:23
【问题描述】:

我的应用中有核心数据库。在该数据库中,一个模型 CourseEvents 包含超过 250000 条记录,每条记录大约有 5 个字段。

一个 UITableViewCell 的每个记录值。

但我不想在单个提取请求中提取所有记录。想根据 UITableView 的滚动来获取 N 条记录。

例子:

当table view第一次加载要获取20条记录时,每当用户滚动UITableView时需要获取下20条记录,比如需要根据UITableview的滚动从模型中获取数据。

我怎样才能做到这一点。请帮忙.....

【问题讨论】:

  • 您的数据是如何排序的?数据是否有可预测的分布? (即按数据排序,每小时有一个条目?)
  • @JonRose 感谢您的回复.. 我这样做我担心的是在加载 tableview 时它应该先获取 200(例如),当用户向上滚动并达到 150 单元格然后需要获取next 200 (201-400),如果用户再次向下滚动,需要获取前 200 (1 - 200)。就像我的 fetch 应该与 UITableView 可见单元格同步。

标签: ios objective-c iphone core-data


【解决方案1】:

试试这个:

-(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
        }
    }
}

【讨论】:

  • 感谢您的回复...您能否将完整代码发送给我。我想在第一次加载表视图时想要获取 20 条记录,每当用户滚动 UITableView 时都需要获取下 20 条记录,比如需要根据 UITableview 的滚动从模型中获取数据。
  • 你从api获取数据了吗?
  • @KKRocks..YES 我从 API 获取数据..我想要这样..我担心加载 tableview 时它应该先获取 200(例如),当用户向上滚动并达到 150单元格然后需要获取下一个 200 (201-400),如果用户再次向下滚动,需要获取上一个 200 (1 - 200)。就像我的 fetch 应该与 UITableView 可见单元格同步。
  • 那么你需要在服务器端设置分页代码。你只需要传递 pagenumber 。
  • 我的疑问是,例如设备中的网络不可用。所以设备处于离线模式。在离线模式下我想从核心数据中获取数据..所以在这种情况下如何实现这个东西。请指导我
【解决方案2】:
//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]

    }

【讨论】:

  • 你能把完整的代码发给我吗?例如,我有标题、图像和核心数据。我想在加载 tableview 时它应该先获取 200(例如),当用户向上滚动并到达 150 个单元格然后需要获取下一个 200(201-400),如果用户再次向下滚动,需要获取前 200 个(1 - 200)。就像我的 fetch 应该与 UITableView 可见单元格同步
  • 非常感谢.. 兄弟非常感谢..实际上我想要一个小例子,比如使用核心数据在 uitableviewcell 中异步显示标题和图像.. 请给我相关代码.... ...
  • 这是一个不同的问题,你应该提出一个新的问题。
【解决方案3】:

@satya 穆尔蒂 尝试这个:- uitableviewcell 异步使用核心数据。

    - (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;
        }

【讨论】:

  • @satyamurty 如果适合您,请您投票 +1。
  • 感谢您的回复。但实际上我想要这样..例如,我有标题、图像和核心数据。我想在加载 tableview 时它应该先获取 200(例如),当用户向上滚动并到达 150 个单元格然后需要获取下一个 200(201-400),如果用户再次向下滚动,需要获取前 200 个(1 - 200)。就像我的 fetch 应该与 UITableView 可见单元格同步
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多