带有 MagicalRecord 庞大数据集的 UItableView [英] UItableView with MagicalRecord huge data set

查看:27
本文介绍了带有 MagicalRecord 庞大数据集的 UItableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储一些数据的表.假设数据太大而无法一次加载到内存中.我想在 UItableView 中显示这些数据.

I have a table that stores some data. Let's say data is too big to load into memory all at once. I want to show this data in UItableView.

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return  [Item MR_numberOfEntities];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


// How to get object for this row ???


return cell;

}

我知道的唯一方法是使用

The only way i knew is to load all data into array with

NSArray *items = [Item MR_findAll];

但我不想那样做.用户将显示前 10 行,为什么我要从 CoreData 加载所有这些行.有什么办法可以用MagicalRecord一一获取吗?

But i don't want to do that. User will be shown first 10 rows, and why should i load all them from CoreData. Is there any way to fetch them one by one using MagicalRecord?

推荐答案

根据 docs 您需要初始化获取请求,并且您可能希望根据滚动进度设置获取偏移量..但您必须手动跟踪它.这是如何实现的基本方法.附注.我还没有测试过这段代码.我只是在文本编辑器中写的:).但它应该按您的要求工作.例如加载限制为 10 的项目.

According to docs you need to init fetch request and also you may want to set the fetch offset depending on scroll progress.. but you will have to trace it manually. Here is a basic approach how it can be achieved. PS. I haven't tested this code . I just wrote it in the text editor :). but it should work as you requested. e.g loading items with limit 10.

    @property (nonatomic) int itemCount;

    @property (nonatomic, strong)  NSMutableArray * items 

    static const int FETCH_LIMIT = 10;


       -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
       {
        return  _itemCount;
       }

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            // Classic start method
            static NSString *cellIdentifier = @"MyCell";
            MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (!cell)
            {
                cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MainMenuCellIdentifier];
            }

            MyData *data = [self.itemsArray objectAtIndex:indexPath.row];
            // Do your cell customisation
            // cell.titleLabel.text = data.title;

            if (indexPath.row == _itemCount - 1)
            {
                [self loadMoreItems];
            }
        }

    -(void)loadMoreItems{

      int newOffset = _itemCount+FETCH_LIMIT; // Or _itemCount+FETCH_LIMIT+1 not tested yet

      NSArray * newItems = [self requestWithOffset: newOffset];

     if(!newItems || newItems.count == 0){

        // Return nothing since All items have been fetched

       return;
     }

      [ _items addObjectsFromArray:newItems ];
    // Updating Item Count
      _itemCount = _items.count;

    // Updating TableView
       [tableView reloadData];
    }

    -(void)viewDidLoad{

     [super viewDidLoad];
          _items = [self requestWithOffset: 0];
          _itemCount =  items.count;
    }

    -(NSArray*)requestWithOffset: (int)offset{

          NSFetchRequest *itemRequest = [Item MR_requestAll];
          [itemRequest setFetchLimit:FETCH_LIMIT]
          [itemRequest setFetchOffset: offset]
          NSArray *items = [Item MR_executeFetchRequest:itemRequest];
       return items;
    } 

希望对你有帮助:)

这篇关于带有 MagicalRecord 庞大数据集的 UItableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆