Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5964819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:30:08+00:00 2026-05-22T19:30:08+00:00

I have the standard fetchedResultsController method and delegate methods in my viewController but they

  • 0

I have the standard fetchedResultsController method and delegate methods in my viewController but they are not being called. I tested this by putting an NSLog in this method and the console never shows this.

I made sure to add FetchedResultsController delegate to .h and .m.

Any ideas why?

Interface

 @interface LogViewController : UIViewController <NSFetchedResultsControllerDelegate, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSArray *logArray;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) Session *session;

@property (nonatomic, retain) IBOutlet UITableView *logTableView;

@end

Implementation

#import "LogViewController.h"

@implementation LogViewController

@synthesize fetchedResultsController = __fetchedResultsController;
@synthesize managedObjectContext;
@synthesize logArray;
@synthesize logTableView;
@synthesize imageView;
@synthesize session;

- (void)dealloc
{
    [logArray release];
    [logTableView release];
    [session release];
    [__fetchedResultsController release];
    [managedObjectContext release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"Log";
    logTableView.backgroundColor = [UIColor clearColor];
    logTableView.separatorColor = [UIColor blackColor];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:24/255.0 green:83/255.0 blue:170/255.0 alpha:1.0];
    self.logArray = [[NSArray alloc]initWithObjects:@"Today's Workout", @"Last Workout", @"Past Week", @"Past Month", @"All Workouts", nil];

    if (managedObjectContext == nil)
    {
        self.managedObjectContext = [(CurlAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    }
}

- (void)viewDidUnload
{
    self.logArray = nil;
    self.logTableView = nil;
    [super viewDidUnload];
}

#pragma mark - Table view data source


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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.logArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    TDBadgedCell *cell = [[[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.textColor = [UIColor blackColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = [logArray objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    UIImageView *myImageView = nil;
    if (indexPath.row == 0)
    {
        myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"customcell_background_top.png"]];
    }
    else if (indexPath.row == 4)
    {
        myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"customcell_background_bottom.png"]];
    }
    else
    {
        myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"customcell_background_middle.png"]];
    }
    [cell setBackgroundView:myImageView];
    [myImageView release];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMM d, y"];
    NSDate *date = nil;
    if (indexPath.row == 0)
    {
        date = [NSDate date];
        NSString *dateString = [dateFormatter stringFromDate:date];
        cell.badgeString = dateString;
    }
    else if (indexPath.row == 1)
    {
        self.session = [[__fetchedResultsController fetchedObjects]lastObject];
        NSDate *date = self.session.timeStamp;
        NSString *dateString = [dateFormatter stringFromDate:date];
        cell.badgeString = dateString;
    }
    else if (indexPath.row > 1)
    {
        cell.badgeString = [NSString stringWithFormat:@"%i", [[__fetchedResultsController fetchedObjects]count]];
    }
    cell.badgeColor = [UIColor colorWithRed:24/255.0 green:83/255.0 blue:170/255.0 alpha:1.0];
    [dateFormatter release];

    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// [cell setBackgroundColor:[UIColor colorWithRed:209/255.0 green:209/255.0 blue:209/255.0 alpha:1.0]];
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.row == 0 || indexPath.row == 1)
    {
        SessionViewController *detailViewController = [[SessionViewController alloc] initWithNibName:@"SessionViewController" bundle:nil];
        detailViewController.title = [logArray objectAtIndex: indexPath.row];
        [self.navigationController pushViewController:detailViewController animated:YES];
        [detailViewController release];
    }
    else
    {
        LogResultsViewController *detailViewController = [[LogResultsViewController alloc] initWithNibName:@"LogResultsTableViewController" bundle:nil];
        detailViewController.title = [logArray objectAtIndex: indexPath.row];
        [self.navigationController pushViewController:detailViewController animated:YES];
        [detailViewController release];
    }
}

#pragma mark - Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController
{
    if (__fetchedResultsController != nil)
    {
        return __fetchedResultsController;
    }

    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Session" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error])
    {
        /*
        Replace this implementation with code to handle the error appropriately.
        abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
        */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    return __fetchedResultsController;
    NSLog(@"Number of Objects = %i",
          [[__fetchedResultsController fetchedObjects] count]);

}

#pragma mark - Fetched results controller delegate


- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    [self.logTableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
    switch(type)
    {
        case NSFetchedResultsChangeInsert:
            [self.logTableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

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

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView = self.logTableView;

    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)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.logTableView endUpdates];
}

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:30:09+00:00Added an answer on May 22, 2026 at 7:30 pm

    replace calls like this

    self.session = [[__fetchedResultsController fetchedObjects]lastObject];
    

    with this

    self.session = [[self.fetchedResultsController fetchedObjects]lastObject];
    

    I.E. Use the getter that lazily loads the NSFetchedResultsController.
    If you use lazy loading you have to use the getter, because the instance variable will stay nil if you never invoke the getter that creates the object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've heard they have standard GUI guideline, but I can't seem to find a
Many languages have standard repositories where people donate useful libraries that they want others
I have some standard-texts, but some portion of it is different. But of these
I have standard view controller acting as the delegate for a modal view controller
I have a web application that have standard form authentication declared, like this: <login-config>
I have a standard django setup using postgres, but I also want to access
I have standard ASP.NET 4 application. Inside this application there are also few razor
I have standard databindings setup for all my TextBoxes to an Object like this:
Ok, So I have standard sencha-touch panel, and because this application needs to be
Could someone explain this for me? I have standard relations in my MSSQL DB:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.