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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:05:30+00:00 2026-06-18T05:05:30+00:00

I get runtime errors which seem to result from my incorrect implementation of GCD

  • 0

I get runtime errors which seem to result from my incorrect implementation of GCD in combination with my custom NSManagedObjects.

Nested in a GCD call, I am using custom NSManagedObjects which (seem to) have their own managed object contexts (= self.managedObjectContext).

I am creating the managed object context in the app delegate by using the managed object context provided by UIManagedDocument: self.managedDocument.managedObjectContext.

I don’t understand how to pass the correct managed object context down to my custom NSManagedObjects. How would I need to change my code to use the correct managed object context?

This is my main method (inside a view controller):

dispatch_queue_t queue;
queue = dispatch_queue_create("queue", NULL);
dispatch_async(queue, ^{
// ...
NSDecimalNumber *value = [reportedPeriod 
   valueForCoa:figure.code 
   convertedTo:self.currencySymbol];
// ...});
}

In this main method I do not have any reference to a managed object context, I do just call valueForCoa:convertedTo: (which is coded as follows):

- (NSDecimalNumber*)valueForCoa:(NSString*)coaStr
convertedTo:(NSString*)targetCurrencyStr {
// ...
CoaMap *coa = [CoaMap coaItemForString:coaStr
   inManagedObjectContext:self.managedObjectContext];
// ...
}

valueForCoa is a method in my custom subclassed NSManagedObject ReportedPeriod and uses its (default) managed object context self.managedObjectContext.

The app then usually crashes in the custom subclassed NSManagedObject CoaMap in the following method when it executes the fetch request:

+ (CoaMap*)coaItemForString:(NSString*)coaStr 
inManagedObjectContext:(NSManagedObjectContext*)context {

NSFetchRequest *request = [NSFetchRequest 
fetchRequestWithEntityName:NSStringFromClass([self class])];
NSPredicate *predicate = 
   [NSPredicate predicateWithFormat:@"coa == %@",coaStr];
   request.predicate = predicate;
// ** The runtime error occurs in the following line **
NSArray *results = [context executeFetchRequest:request error:nil];
// ...
}

The error message is: Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFSet: 0x9a8a4a0> was mutated while being enumerated.

Could you please help me with this issue and give me some suggestions on how to improve my code to pass the correct managed object contexts (or on how to make sure that the correct context is used in all methods)?

Thank you very much!

  • 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-06-18T05:05:31+00:00Added an answer on June 18, 2026 at 5:05 am

    That error generally relates to using a managed object incorrectly context across different threads or queues. You created the MOC on the main queue, but you’re using it on a background queue without considering that fact. It’s not wrong to use the MOC on a background queue, but you need to be aware of that and take preparations.

    You didn’t say how you’re creating the MOC. I suggest that you should be doing this:

    NSManagedObjectContext *context = [[NSManagedObjectContext alloc]
        initWithConcurrencyType: NSMainQueueConcurrencyType];
    

    With main queue concurrency you can just use it normally on the main thread. When you’re in your dispatch queue though, do this:

    [context performBlockAndWait:^{
        NSFetchRequest *request = [NSFetchRequest 
            fetchRequestWithEntityName:NSStringFromClass([self class])];
        NSPredicate *predicate = 
           [NSPredicate predicateWithFormat:@"coa == %@",coaStr];
        request.predicate = predicate;
        NSArray *results = [context executeFetchRequest:request error:nil];
        // ...
    }];
    

    This will ensure that the MOC’s work occurs on the main thread even though you’re on a background queue. (Technically what it actually means is that the MOC’s work in the background will be correctly synchronized with work it does on the main queue, but the result is the same: this is the safe way to do this).

    A similar approach would be to use NSPrivateQueueConcurrencyType instead. If you do that, you’d use performBlock or performBlockAndWait everywhere for the MOC, not just on background threads.

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

Sidebar

Related Questions

I can't seem to get runtime.exec working in my android app. I've tried it
When I get a runtime exception from the standard library, it's almost always a
I developed an application in VB6. In client's environment it raises runtime errors which
Why does the following code get the runtime error: Members of the Triggers collection
If I do this stuff.addEventListener(FooEvent.NAME, function(e:FooEvent) { dispatchEvent(e); } I get a runtime error
In an Android application, whenever a runtime error occurs, is it possible to get
I am trying to update the app.config file at runtime. I get the error
I am having trouble using EnumSet on the client side. I get this runtime
I get an exception thrown at runtime when I set AllowsTransparency=True I get an
Summary: I periodically get a .NET Fatal Execution Engine Error on an application which

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.