2014-09-12 2 views

답변

1

내가 NSFetchedResultsControllerDelegate를 구현하고 있습니다 여기에

는 링크입니다. 나는 "SectionNum"= 0의 온라인 사용자 목록을 얻고 있습니다. 사용자가 갈 때마다 오프라인/온라인 컨트롤러의 대리자 메서드가 호출됩니다. 해당 업데이트 tableView.

// NSFetchedResultsController * fetchedResultsController; // 인스턴스 변수

in viewWillAppear 

//xmpp user array 
    self.xmppUserArray=[[[self fetchedResultsController] fetchedObjects] mutableCopy]; 

    for (int i=0; i<[[self xmppUserArray] count]; i++) { 

     if ([[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"sectionNum"] integerValue]==0) { 
      //this is user is online 
      [[[AKSGetCareerGlobalClass SharedInstance] onlineUserArray] addObject:[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"nickname"]]; 

     } 
    } 


//also implement method 
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 
    //remove previous data or clear array 

    [[self xmppUserArray] removeAllObjects]; 
    [[[AKSGetCareerGlobalClass SharedInstance] onlineUserArray] removeAllObjects]; 


    //get data from core data 
    self.xmppUserArray=[[[self fetchedResultsController] fetchedObjects] mutableCopy]; 


    for (int i=0; i<[[self xmppUserArray] count]; i++) { 

     if ([[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"sectionNum"] integerValue]==0) { 
      //this is user is online 
      [[[AKSGetCareerGlobalClass SharedInstance] onlineUserArray] addObject:[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"nickname"]]; 

     } 
    } 


    [[self msgTableView] reloadData]; 

} 



-(NSFetchedResultsController *)fetchedResultsController { 
    if (fetchedResultsController == nil) 
    { 
     NSManagedObjectContext *moc = [[self appDelegate] managedObjectContext_roster]; 

     NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject" 
                inManagedObjectContext:moc]; 

     NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES]; 
     NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES]; 

     NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil]; 
     //NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES]; 

     //NSString *myJID = [[NSUserDefaults standardUserDefaults] stringForKey:@"userJID"]; 
     //NSLog(@"My JID ====>%@",myJID); 

     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subscription=='both'"];//take care about subscription 


     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
     [fetchRequest setEntity:entity]; 
     [fetchRequest setPredicate:predicate]; 
     [fetchRequest setSortDescriptors:sortDescriptors]; 
     [fetchRequest setFetchBatchSize:20]; 

     fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                     managedObjectContext:moc 
                     sectionNameKeyPath:@"sectionNum" 
                        cacheName:nil]; 
     [fetchedResultsController setDelegate:self]; 


     NSError *error = nil; 
     if (![fetchedResultsController performFetch:&error]) 
     { 
      DDLogError(@"Error performing fetch: %@", error); 
     } 

    } 

    return fetchedResultsController; 
} 
관련 문제