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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:10:20+00:00 2026-05-15T17:10:20+00:00

I have been slowly learning iPhone development and seem to keep hitting walls where

  • 0

I have been slowly learning iPhone development and seem to keep hitting walls where I can’t figure out how to do what I want to do the right way 🙁

Basically, I want a class that handles all interactions with the data layer, for example, getting a mutable array of some list of objects from the data store.

This is pretty trivial in other languages where you have a garbage collector, but in Objective-C on the iPhone, I’m not sure what to do.

This is an example method on a DataFactory class we were creating. Note the comment on where we are not sure when to release….

- (NSMutableArray*)fetchAllDrivers{

    NSMutableArray *results = [[NSMutableArray alloc] init];;

    if (self.appDelegate != nil) {
        NSManagedObjectContext *context = [self.appDelegate managedObjectContext];

        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
        [request setEntity: entity];

        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:NO];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor, nil];
        [request setSortDescriptors: sortDescriptors];
        [sortDescriptors release];
        [sortDescriptor release];

        NSError *error;
        results = [[context executeFetchRequest:request error:&error] mutableCopy];
        if (results == nil) {
            //something went wrong
        }

        //Where should this be released???  Certainly not here!
        [results release];

        [request release];
    }
    else {
        [NSException raise:@"Can't fetch b/c app delgate is nil!" format: @"!!!"];    
    }

    return results;
}

Calling code, related to my comment:

NSMutableArray* arr = [dataFactory fetchAllDrivers];
[arr retain];
//Some code  where we use arr
[arr release];
  • 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-15T17:10:20+00:00Added an answer on May 15, 2026 at 5:10 pm

    Following naming conventions, your fetchAllDrivers should return an autoreleased object.

    - (NSMutableArray*)fetchAllDrivers
    {
    
        if (!self.appDelegate) {
            // Big Problems Raise exception immediately if you want...
            return nil; 
        }
    
        NSManagedObjectContext *context = [self.appDelegate managedObjectContext];
    
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
        [request setEntity: entity];
    
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:NO];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor, nil];
        [request setSortDescriptors: sortDescriptors];
        [sortDescriptors release];
        [sortDescriptor release];
    
    
        NSError *error = nil;
    
        NSMutableArray *results = [[NSMutableArray alloc] initWithArray:[context executeFetchRequest:request error:&error] copyItems:YES];
    
        if (error) {
            // Something went wrong
            [results release];
            // Error handling code here
            [request release];
            return nil;
        }
    
        [request release];
        return [results autorelease];
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to figure out why this PL/SQL purge script runs slowly
I'm working on a project right now where I have been slowly accumulating a
I have been learning (slowly but surely) how to do deal with sqlite databases
I'm a complete noob to JQUERY, learning things slowly. But I have been downloaded
I have been slowly learning SQL the last few weeks. I've picked up all
I've been slowly working my way through the examples in Beginning iPhone Development and
I've been slowly churning through my first iPhone app, outside of learning within the
I'm diving into iOS development and have been slowly building my own alarm clock
I'm a Flash developer but have been slowly moving to developing my applications in
Been slowly picking up the basics in IOS Programming, but seemed to have hit

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.