2013-01-16 3 views
0

NSFetchedResultsController이 있는데, 사용자가 새 개체를 만들 때마다 데이터가 저장되는 것을 확인했습니다. 그러나, 나는 아직 업데이 트되지 않는 유일한 것은 섹션과 행의 수를 발견했습니다 NSFetchedResultsController. 왜 그런지 모르겠습니다.섹션 및 행 개수 NSFetchedResultsController

나는 다양한 방법으로 새로운 객체를 생성하는 것을 시도해 보았습니다. 또한 어떤 일이 벌어지고 있는지 보려고 프로세스의 모든 단계를 로깅하려고 시도했지만 실제로 이런 일을 기록 할 수있는 것은별로 없기 때문에 왜 이런 일이 발생했는지에 대한 정보는 거의 남지 않았습니다. .

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    NSLog(@"number of sections: %d", [[self.fetchedResultsController sections] count]); 
    return [[self.fetchedResultsController sections] count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"number of rows: %d", [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects]); 
    return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects]; 
} 

- (void)saveForName:(NSString *)name 
       photo:(NSData *)photo 
     timePosted:(NSDate *)timePosted 
      details:(NSString *)details 
     dateAndTime:(NSDate *)dateTime 
      location:(NSString *)location 
{ 

    if (!self.shindysDatabase) { 
     NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 
     self.shindysDatabase = [[UIManagedDocument alloc] initWithFileURL:url]; 
     NSLog(@"shindy database reset."); 
    } 

    Shindy *shindy = (Shindy *)[NSEntityDescription insertNewObjectForEntityForName:@"Shindy" inManagedObjectContext:self.shindysDatabase.managedObjectContext]; 

    shindy.name = name; 
    shindy.photo = photo; 
    shindy.timePosted = timePosted; 
    shindy.details = details; 
    shindy.dateAndTime = dateTime; 
    shindy.location = location; 

    NSError *error = nil; 
    [self.shindysDatabase.managedObjectContext save:&error]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    } 

    Shindy *shindy = (Shindy *)[NSEntityDescription entityForName:@"Shindy" inManagedObjectContext:self.shindysDatabase.managedObjectContext]; 
    shindy = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    UIImageView *avatar = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 64, 56)]; 
    // Converting NSData to UIImage 
    avatar.image = shindy.photo; 
    avatar.clipsToBounds = NO; 
    [cell addSubview:avatar]; 

    NSLog(@"avatar: %@", avatar); 

    // The name of the user who posted the shindy 
    UILabel *avatarName = [[UILabel alloc] initWithFrame:CGRectMake(90, 10, 125, 19)]; 
    avatarName.font = [UIFont boldSystemFontOfSize:15]; 
    avatarName.backgroundColor= [UIColor clearColor]; 
    avatarName.text = shindy.name; 
    [cell addSubview:avatarName]; 

    NSLog(@"avatarName: %@", avatarName); 

    // The time the shindy was posted 
    NSDateFormatter *timePostedFormatter = [[NSDateFormatter alloc] init]; 
    [timePostedFormatter setDateFormat:@"mm"]; 
    UILabel *timePosted = [[UILabel alloc] initWithFrame:CGRectMake(280, 10, 30, 20)]; 
    timePosted.font = [UIFont systemFontOfSize:12]; 
    timePosted.textColor = [UIColor lightGrayColor]; 
    timePosted.backgroundColor= [UIColor clearColor]; 
    timePosted.text = [timePostedFormatter stringFromDate:shindy.timePosted]; 
    [cell addSubview:timePosted]; 

    NSLog(@"Time posted: %@", timePosted); 

    // Formatting fetched NSDate into string 
    NSDateFormatter *dateAndTimeFormatter = [[NSDateFormatter alloc] init]; 
    [timePostedFormatter setDateFormat:@"MM/DD/YYYY hh:mm"]; 
    UILabel *dateAndTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(250, 70, 30, 20)]; 
    dateAndTimeLabel.font = [UIFont systemFontOfSize:12]; 
    dateAndTimeLabel.backgroundColor= [UIColor clearColor]; 
    dateAndTimeLabel.text = [dateAndTimeFormatter stringFromDate:shindy.dateAndTime]; 
    [cell addSubview:dateAndTimeLabel]; 

    NSLog(@"date and time: %@", dateAndTimeLabel.text); 

    // Details of the Shindy 
    UILabel *shindyDescription = [[UILabel alloc] initWithFrame:CGRectMake(90, 20, 220 , 50)]; 
    shindyDescription.numberOfLines = 3; 
    shindyDescription.font = [UIFont systemFontOfSize:12]; 
    shindyDescription.backgroundColor = [UIColor clearColor]; 
    shindyDescription.text = shindy.details; 
    [cell addSubview:shindyDescription]; 

    NSLog(@"details: %@", shindyDescription.text); 

    return cell; 
} 

상용구 코드 :

이 내가 객체가 변경 될 때 업데이트를해야 할 단지 코드가 어쨌든, 여기에 코드입니다.

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates. 
    [self.tableView beginUpdates]; 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { 

    UITableView *tableView = self.tableView; 

    switch(type) { 

     case NSFetchedResultsChangeInsert: 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeUpdate: 
      [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
      break; 

     case NSFetchedResultsChangeMove: 
      [tableView deleteRowsAtIndexPaths:[NSArray 
               arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView insertRowsAtIndexPaths:[NSArray 
               arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 

    switch(type) { 

     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates. 
    [self.tableView endUpdates]; 
} 

답변

0

나는 오랫동안 NSFetchedResultController 작동하지 않은,하지만 첫눈에 내가 말할 것이다 :
Where are you asking your table view to update itself?
나는 결과 컨트롤러가 테이블 업데이트 자체를 실행할 것이라고 생각하지 않습니다.


은 그냥 NSFetchedResultsController 문서에 있었고, 나는이를 본 적이 :
가 선택적으로 관리 W 연관된 개체에 변경 사항을 모니터링 :

또한

, 가져온 결과 컨트롤러는 다음과 같은 기능을 제공합니다 개체 컨텍스트를 만들고 결과 집합의 변경 내용을 해당 대리인에게보고하십시오 ( "컨트롤러의 대리인"참조).

그리고 또한 NSFetchedResultsControllerDelegate 문서

에 그냥 controllerDidChangeContent 구현할 수 : 테이블 뷰를 다시로드 ( 보류중인 모든 변경 사항이 처리 된 대리자로 전송됩니다).

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath 
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type 

를 그냥 수행하십시오 : 그냥 디버깅 목적으로


, 당신은 제거하려고이 나는이 솔루션입니다 말하는 게 아니에요

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    [self.tableView reloadData]; 
} 

하지만, 결과가 같은지 알아 보는 것은 흥미로울 것입니다.

+0

나는 이미 다른 곳에서 내 코드에있는 것들을 모두 셋업했다. 이 코드는 일종의 "기본"코드로 간주되기 때문에 게시하지 않아도됩니다. – jakenberg

+0

"기본"코드로 얼마나 많은 실수가 있었는지보고 놀랍니다. –

+0

Apple 문서에서 복사하여 붙여 넣습니다. 하지만 당신이 정말로 그것을보고 싶어, 그래서 그것을 보여 내 질문을 편집했습니다 :) – jakenberg

0

NSFetchedResultsController 개체를 만들 때 sectionNameKeyPath를 지정 했습니까? nil을 사용하면 단 하나의 섹션 만 생성됩니다.

(instancetype) initWithFetchRequest (NSFetchRequest *) fetchRequest managedObjectContext (NSManagedObjectContext *) 컨텍스트 sectionNameKeyPath (는 NSString *) sectionNameKeyPath cacheName (는 NSString *) 이름;

sectionNameKeyPath : 섹션 이름을 반환하는 결과 개체의 키 경로입니다. nil을 전달하여 컨트롤러가 단일 섹션을 생성해야 함을 나타냅니다.

관련 문제