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

  • SEARCH
  • Home
  • 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 7542739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:14:46+00:00 2026-05-30T08:14:46+00:00

I’ve ran into a puzzling issue: I have only one method defined for inserting

  • 0

I’ve ran into a puzzling issue:

I have only one method defined for inserting objects into Core Data. I’m trying to insert data in 2 different ways: one is from a UITableView with the + button. The other is from a regular UIViewController by calling the method below.

At the end of the method, the method returns a NSManagedObject in both cases, but the fetchedObjects count is incremented only in the case of inserting from a UITableViewController.

If I load the UITableViewController first, all subsequent calls to the method correctly increment the number of fetched objects. If I do not do so, the number stays the same, even though NSManagedObjects are returned from the method. On subsequent app restart, the “invisible” objects are loaded again and become visible

It seems to me that the issue is with the ManagedObjectContext, but I do not see where I incorrectly set it.

The project uses iOS5, ARC and storyboards.

-(Event*)insertAndReturnNewEventWithSeverity:(int)type
{
    // Create a new instance of the entity managed by the fetched results controller.
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];

    Event *newManagedObject = (Event*)[NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];


    newManagedObject.insert_datetime = [NSDate date];



    NSError *error = nil;
    if (![context save:&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. 
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        //        abort();
    }

    NSLog(@"objects: %i", self.fetchedResultsController.fetchedObjects.count );

    return newManagedObject;
}

There’s only one managedObject context, which is initialized within the app delegate.

-(NSManagedObjectContext*)managedObjectContext
{
    if(__managedObjectContext!=nil)
    {
        return  __managedObjectContext;
    }else{

        self.managedObjectContext = self.appDelegate.managedObjectContext;    
    }
    return __managedObjectContext;
}

UPDATE:
Adding my UIViewController as a delegate to the NSFetchedResultsController, and adding didChangeObject and didChangeSection causes the .fetchedObjects property to correctly increment itself!

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.coreDataManager.fetchedResultsController.delegate = self;
}

    - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
           atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
          newIndexPath:(NSIndexPath *)newIndexPath
    {
        NSLog(@"Changed object");
    }

    - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
               atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
    {
      NSLog(@"Changed section");
    }

Thank you for your help!

  • 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-30T08:14:47+00:00Added an answer on May 30, 2026 at 8:14 am

    NSFetchedResultsController requires that a delegate be set to monitor changes in fetch results.

    A controller thus effectively has three modes of operation, determined
    by whether it has a delegate and whether the cache file name is set.

    No tracking: the delegate is set to nil. The controller simply
    provides access to the data as it was when the fetch was executed.

    > Memory-only tracking: the delegate is non-nil and the file cache name
    is set to nil. The controller monitors objects in its result set and
    updates section and ordering information in response to relevant
    changes.

    Full persistent tracking: the delegate and the file cache name are
    non-nil. The controller monitors objects in its result set and updates
    section and ordering information in response to relevant changes. The
    controller maintains a persistent cache of the results of its
    computation.

    https://developer.apple.com/library/ios/#documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

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.