Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ashwinr87

macrumors member
Original poster
Mar 9, 2011
81
0
I am using multithreading while loading data from the database.

I am doing the following

Code:
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
                         });
                   }
                });

and my searchObjectsInUserContext code is like this

Code:
+(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?
 
Last edited:
After the if the checks for mutableFetchResults being nil, put an if that checks for mutableFetchResults's count being zero. Inside that if, use NSLog to output details of your variables. You might even want to put a breakpoint inside the if so you can check the variables in the debugger.

Does every thing looks as expected when mutableFetchResults's count is zero? I'm thinking particularly of the predicate.
 
After the if the checks for mutableFetchResults being nil, put an if that checks for mutableFetchResults's count being zero. Inside that if, use NSLog to output details of your variables. You might even want to put a breakpoint inside the if so you can check the variables in the debugger.

Does every thing looks as expected when mutableFetchResults's count is zero? I'm thinking particularly of the predicate.

actually I did exactly that and the mutableFetchResults is indeed zero. I have updated my entire code with what I use. Please see if that helps.

I noticed that if I reload the table, the values get displayed. is it maybe that I am not reloading it properly the first time?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.