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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:26:58+00:00 2026-05-26T03:26:58+00:00

I am using multithreading while loading data from the database. I am doing the

  • 0

I am using multithreading while loading data from the database.

I am doing the following

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
               ^{
                   // Get data
                   NSDate *pastDate = [CommonHelper getSelectedDateYearBackComponents:[NSDate date]];
                   NSPredicate *predicate = [NSPredicate predicateWithFormat:@"users == %@ && startDate >= %@", objUser,pastDate];
                   NSMutableArray *fetchArray = [DataAccess searchObjectsInUserContext:@"userIngo" :predicate :@"startDate" :NO];

                   if ([fetchArray count] > 0)
                   {
                       dispatch_async(dispatch_get_main_queue(), 
                                     ^{
                                         // Reload table
                                         [self.tableView reloadData]; });
                   }

                   else 
                   {
                       dispatch_async(dispatch_get_main_queue(), 
                        ^{  // calling Webservice
                         });
                   }
                });

where users is the entity from which I am trying to fetch data and objUser is the user object for whom I am retrieving data from the users entity

and my searchObjectsInUserContext code is like this

+(NSMutableArray *) searchObjectsInLabContext: (NSString*) entityName : (NSPredicate *) predicate : (NSString*) sortKey : (BOOL) sortAscending
 {
i3EAppDelegate *appDelegate = (i3EAppDelegate *)[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setUndoManager:nil];
[context setPersistentStoreCoordinator:[appDelegate persistentStoreCoordinator]];

// Register context with the notification center
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(mergeChanges:) name:NSManagedObjectContextDidSaveNotification object:context];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
[request setEntity:entity]; 
[request setFetchBatchSize:10];
[request setReturnsObjectsAsFaults:NO];

// If a predicate was passed, pass it to the query
if(predicate != nil)
{
    [request setPredicate:predicate];
}

// If a sort key was passed, use it for sorting.
if(sortKey != nil)
{
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:sortAscending];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptors release];
    [sortDescriptor release];
}

NSError *error;

NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];

if (mutableFetchResults == nil) {

    // Handle the error.
    // NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    exit(-1);  // Fail

}

[request release];
appDelegate = nil;

return [mutableFetchResults autorelease];
  }

So in my searchObjectInUserContext I use multiple managedObjectContext so that it does not create problems for me while multithreading as explained by Fred McCann in his blog post.

However, my problem is at NSMutableArray *mutableFetchResults in my searchObjectsInUserContext because it returns 0 at times even though there is data in the database.

Can someone help me with what I am doing wrong?

  • 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-26T03:26:59+00:00Added an answer on May 26, 2026 at 3:26 am
    1. You are leaking the context you create; you never release it.
    2. There is no need to register as an observer because you are never saving with that context.
    3. Why are you making a mutable copy of the context? That rarely serves any useful purpose.
    4. How do you know there is data in the database?
    5. Is this method being run on a background thread?

    Update

    There is nothing wrong with your fetch so I suspect the issue might be one of timing. Is this fetch being run before another thread saves? That would explain the behavior you are seeing.

    Also, why are you running this on a background thread? Are you seeing a performance issue that requires a background search like this?

    Update 2

    First, I still question the need for the background fetching. That is normally reserved for when you have performance issues as fetching is very fast.

    Second, you fetch on a background queue but then you don’t do anything with the data you fetched. You do not hand it off to the table (which would be bad anyway) you just fetch and throw it away.

    If you are fetching just to count then don’t fetch, do a -[NSManagedObjectContext -countForFetchRequest: error:]. It will be even faster and removes the need for the background queue call.

    If you are expecting to do something with the results you have an issue. Instances of NSManagedObject cannot cross a thread/queue boundary. So fetching on the background queue does very little for the UI. You need to fetch those objects on the main queue/main thread. If you don’t you will crash because Core Data does not allow it.

    In the end, your background queues are doing you more harm than good and they are not solving the problem you are trying to solve.

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

Sidebar

Related Questions

In my iPad application, I am using Multithreading to read data from my database
I have to write this produce consumer application using multithreading. I wrote the following
I have an application which performs 30 independent tasks simultaneously using multithreading, each task
In my multithreading application I am using some variables that can be altered by
I'm using multithreading in my application with _beginthread and right now to wait until
I want to implement a multithreading environment using Qt4. The idea is as follows
I am working on maintaining someone else's code that is using multithreading, via two
While there are many samples around about using OpenGL ES on Android all of
This is my first real attempt at using multithreading, I want to know how
I am developing a multithreading application using BackroundWorker. In the Do_Work method I call

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.