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 1027073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:11:02+00:00 2026-05-16T12:11:02+00:00

in my app I need loop through all my entities in Core Data and

  • 0

in my app I need loop through all my entities in Core Data and I’m using NSFetchedresultcontroller.

I’m doing it like this at the moment:

NSArray *tempArray = [[NSArray alloc] initWithArray:self.fetchedResultsController.fetchedObjects];

for (MyClass *item in tempArray)
{
    // do something
}

[tempArray release]; tempArray = nil;

Is there any better way to do it without creating the tempArray?

Thanks a lot

  • 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-16T12:11:02+00:00Added an answer on May 16, 2026 at 12:11 pm

    Depends on what you want to do. If you are just changing a value then yes, there is an easier way:

    [[[self fetchedResultsController] fetchedObjects] setValue:someValue forKey:@"someKey"]
    

    Which would loop through all of the objects setting the value. This is a standard KVC operation. Note that this will expand memory as each entity will get realized during the mutation.

    If you need to do something far more involved with each entity or you are running into a memory issue then things get a bit more complicated. NOTE: Do not worry about memory until the optimization stage of coding. Pre-optimization of memory issues, especially with Core Data, is a waste of your time.

    The concept is that you will loop over each entity and change it as needed. In addition, at a certain point you should save the context, reset it and then drain a local autorelease pool. This will reduce your memory usage as you will push the objects you just manipulated back out of memory before you pull the next batch in. For example:

    NSManagedObjectContext *moc = ...;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSInteger drainCounter = 0;
    for (id object in [[self fetchedResultsController] fetchedObjects]) {
      //Do your magic here
      ++drainCounter;
      if (drainCounter = 100) {
        BOOL success = [moc save:&error];
        NSError *error = nil;
        NSAssert2(!success && error, @"Error saving moc: %@\n%@", [error localizedDescription], [error userInfo]);
        [moc reset];
        [pool drain], pool = nil;
        pool = [[NSAutoreleasePool alloc] init];
        drainCounter = 0;
      }
    }
    
    BOOL success = [moc save:&error];
    NSError *error = nil;
    NSAssert2(!success && error, @"Error saving moc: %@\n%@", [error localizedDescription], [error userInfo]);
    [pool drain], pool = nil;
    

    This will keep memory usage down but it is expensive!! You are hitting disk after every 100 objects. This should only be used after you have confirmed that memory is an issue.

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

Sidebar

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.