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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:26:43+00:00 2026-05-31T10:26:43+00:00

I have a simple function inside a NSManagedObjectContext category to help me get results

  • 0

I have a simple function inside a NSManagedObjectContext category to help me get results from my moc. I’m executing the results inside the performBlockAndWait block supplied from the moc instance. I need to return an autoreleased array, and normally the executeFetch from the moc already does that. But in this case I need to retain the array inside the block even though I declare the array with the __block directive. It looks like it gets released once the block is finished.
Is this normal?
Thank you.

-(NSArray*)executeFetchWithEntityName:(NSString*)entityName predicate:(NSPredicate*)predicate{
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self];
if (!entity) {
    NSLog(@"entity is nil in executeFetchWithEntityName: %@", entityName);
    return [[[NSArray alloc]init]autorelease];
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
[fetchRequest setEntity:entity];
if(predicate){
    [fetchRequest setPredicate:predicate];   
}
__block NSError *error = nil;
__block NSArray *array = nil;
[self  performBlockAndWait:^{
    array = [self executeFetchRequest:fetchRequest error:&error];
    [array retain]; <------ IF I DONT RETAIN HERE, IT CRASHES FURTHER ON
}];

NSLog(@"retain count: %i", array.retainCount);

if (error) {
    NSLog(@"error");
    array = nil;
}

[fetchRequest release];
return [array autorelease];

}

  • 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-31T10:26:45+00:00Added an answer on May 31, 2026 at 10:26 am

    I suspect that performBlockAndWait has an internal autorelease pool wrapping execution of the block. As such, the array returned by executeFetchRequest:error: is released when you leave the block, which is why you need to retain it.

    Note that this wouldn’t be an issue under ARC; the array pointer would retain the array automatically. If you’ve got the option, I’d recommend switching to ARC. But in the meantime, now you know what’s going on.

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

Sidebar

Related Questions

I have a simple function GetPageName(String PageFileName, String LangCode) defined inside a class file.
I have a simple function called a lot. Inside this function, I have many
I have a simple function that counts from 0 to 5000 and does funky
I have the simple javascript function inside $(function() { ... }); body var uploader
I have another simple one. How to make a long decode inside a function
I have a simple function in which an array is declared with size depending
I have a simple function that I want to call in the code behind
I have a simple function written in Oracle 9i (version 9.2.0.4.0) to simulate an
I have a simple function to open an editor: open_an_editor() { nano $1 }
I have a simple function that returns an NSString after decoding it. I use

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.