Search code examples
iosobjective-ccore-datansmanagedobjectcontextexecutefetchrequest

Application freeze while executing fetch request


In my application, while executing fetch request application get freeze randomly. I have tried with multiple Choice like @synchronized and performblock still hang occur. below is my first fetch request block. Application hang in this fetch request randomly.

+(BXXXX *)getDetailsById:(NSNumber *)Id
    {
        NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
        NSEntityDescription *entityDescription = [NSEntityDescription

                                                  entityForName:@"BXXXX"  inManagedObjectContext:[SDataManager managedObjectContext]];
        [fetch setEntity:entityDescription];
        [fetch setPredicate:[NSPredicate predicateWithFormat:
                             @"(BId = %@)",Id]];


        __block NSArray *bDetails;
        [[SDataManager managedObjectContext] performBlockAndWait:^{
            NSError *error = nil;
            bDetails = [[SDataManager managedObjectContext] executeFetchRequest:fetch error:&error];

        }];

        if([bDetails count] == 1)
            return [bDetails objectAtIndex:0];
        else
            return nil;

    }

//MY Managed object context declaration

+(NSManagedObjectContext *)managedObjectContext
{
    static NSManagedObjectContext *managedObjectContext;
    if(managedObjectContext!=nil){
        return managedObjectContext;
    }
    @try { 
        NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
        if (coordinator != nil) {
            managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
            [managedObjectContext setPersistentStoreCoordinator: coordinator];
        }
    }
    @catch (NSException *exception) {
        NSLog(@"Exception occur %@",exception);
    }
        return managedObjectContext;

}

Please guide me to fix this issue. I tried hard but i cant fix this issue still now.


Solution

  • I have fixed this issue by creating two managed object context like this.
    // Base
        +(NSManagedObjectContext *)managedObjectContext
        {
            static NSManagedObjectContext *managedObjectContext;
            if(managedObjectContext!=nil){
                return managedObjectContext;
            }
            @try { 
                NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
                if (coordinator != nil) {
                    managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
                    [managedObjectContext setPersistentStoreCoordinator: coordinator];
                }
            }
            @catch (NSException *exception) {
                NSLog(@"Exception occur %@",exception);
            }
                return managedObjectContext;
    
        }
    
    //Child
    
        +(NSManagedObjectContext *)childManagedObjectContext
        {
            static NSManagedObjectContext *managedObjectContext;
            if(managedObjectContext!=nil){
                return managedObjectContext;
            }
            @try {
                NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
                if (coordinator != nil) {
                    managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
                    [managedObjectContext setPersistentStoreCoordinator: coordinator];
                }
            }
            @catch (NSException *exception) {
                NSLog(@"Exception occur %@",exception);
            }
            return managedObjectContext;
    
        }
    

    for executing the fetch request i have used child managed object context with Concurrency Type as NSPrivateQueueConcurrencyType. It working fine for me. Now UI hang is not there.