2014-04-14 3 views
2

다음 기능을 구현하고 싶습니다. 어떤 도움을 주실 수 있습니까?iOS 앱에서 UITableView 스크롤링과 CoraData 가져 오기를 동기화하는 방법은 무엇입니까?

내 앱에 핵심 데이터베이스가 있습니다. 이 데이터베이스에서 한 모델 CourseEvents은 150000 개 이상의 레코드를 포함하고 각 레코드는 약 12 ​​개의 필드를 포함합니다.

각 레코드 값은 UITableViewCell입니다.

하지만 단일 가져 오기 요청으로 모든 레코드를 가져오고 싶지 않습니다. 스크롤 수에 따라 N 레코드를 가져 오려고합니다.

예 :

테이블 뷰로드 처음 사용자가 UITableViewUITableview의 스크롤에 기반 모델에서 데이터를 가져올 필요가처럼, 다음 (200) 기록을 가져 오기 위해 필요 스크롤 할 때마다 200 개 레코드를 가져 오기 할 때.

어떻게해야합니까? 친절하게 도와주세요 .....

+0

지금까지 해본 내용은 무엇입니까? 이미'NSFetchRequest'의 fetchLimits와 fetchOffset을 살펴 보았습니까? – Volker

+0

나는 fetchlimit을 알고있다. 하지만 나는 fetchoffset을 찾지 않았다. 하지만 우려가 처음 200 (예를 들어), 사용자가 스크롤 및 150 다음 다음 200 (201-400) 가져올 필요가 도달 할 때 테이블 뷰를로드 할 때 사용자가 다시 아래로 스크롤 할 경우, 이전 200 가져올 필요가 1 - 200). 내 가져 오기처럼'UITableView' 보이는 셀과 동기화해야합니다. – Nagarajan

답변

3

질문을 올바르게 이해하면 처음에는보기를로드 할 때 200 개의 레코드 만 가져오고 tableView 스크롤에서는 다음 200 개를 가져 오는 등의 작업을 수행합니다. 핵심 데이터를 사용하고 있다면 NSFetchedResultsController의 도움으로 레코드를 가져 오는 것이 더 쉽습니다. setFetchBatchSize을 가져 오려는 레코드로 설정하면됩니다. 20 개는 귀하의 경우에도 좋을 것입니다. 사용 가능한 온라인 및 사과 샘플도 매우 다양합니다. 다음은 CoreDataBooks 예의 링크입니다. 이것은 NSFetchedResultsController를 사용하는 방법에 관해서는 tutorial입니다. 그리고 마지막으로 Core Data Programming guide이 도움을 드릴 것입니다. 여기에 NSFetchedResultController를 사용하는 방법에 대한 샘플 코드가 있습니다.

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSError *error; 
    if (![[self fetchedResultsController] performFetch:&error]) { 
     // Update to handle the error appropriately. 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     exit(-1); // Fail 
    } 


} 

- (NSFetchedResultsController *)fetchedResultsController { 

    if (_fetchedResultsController != nil) { 
     return _fetchedResultsController; 
    } 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription 
     entityForName:@"FailedBankInfo" inManagedObjectContext:managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
     initWithKey:@"details.closeDate" ascending:NO]; 
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; 

    [fetchRequest setFetchBatchSize:20]; 

    NSFetchedResultsController *theFetchedResultsController = 
     [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
      managedObjectContext:managedObjectContext sectionNameKeyPath:nil 
      cacheName:@"Root"]; 
    self.fetchedResultsController = theFetchedResultsController; 
    _fetchedResultsController.delegate = self; 

    return _fetchedResultsController; 

} 

- (NSInteger)tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section { 
    id sectionInfo = 
     [[_fetchedResultsController sections] objectAtIndex:section]; 
    return [sectionInfo numberOfObjects]; 
} 

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
    EntityName *entity = [_fetchedResultsController objectAtIndexPath:indexPath]; 
    cell.textLabel.text = entity.name; //just example 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@", 
     entity.city, entity.state]; 
} 

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = 
     [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // Set up the cell... 
    [self configureCell:cell atIndexPath:indexPath]; 

    return cell; 
} 
+0

당신은 설명 할 수 있습니다 다음 코드는 다음 200 레코드가 내 tableview 스크롤하는 동안 데이터베이스에서 가져 오는 중입니다. – Nagarajan

+0

NSFetchedResultsController는 코어 데이터 저장소에서 20 개 항목의 하위 집합을 가져 오는 작업을 처리하고 테이블 뷰를 스크롤 할 때 더 많이 가져옵니다. 데이터 가져 오기를 처리하는 데 코드를 넣지 않아도됩니다. NSFetchedResultsCntroller에 의해 내부적으로 처리됩니다. 샘플 코드 또는 튜토리얼 링크를 사용해보십시오. – suhit

+0

예. 알았어요. 고마워. – Nagarajan

1

이 샘플 프로젝트는 제 솔루션을 구현하는 데 많은 도움이되었습니다. 그것은 당신을 역시 도울지도 모른다.

관련 문제