3

I am using Core Data for managing data base into my app.

I can not post the code here, because its too lengthy. But I guess that I can explain my problem in small line of code along with some snap shots.

+(NSArray *)checkusernameandpassword:(NSString *)entityname  username:(NSString *)username   password:(NSString *)password 
{
    managedobjectcontext=[Singleton sharedmysingleton].managedobjectcontext;
    NSEntityDescription *entity=[NSEntityDescription entityForName:entityname inManagedObjectContext:managedobjectcontext];

    NSFetchRequest *request=[[NSFetchRequest alloc] init];
    [request setEntity:entity];

    NSPredicate *predicates=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"userName==\"%@\" AND password==\"%@\"",username,password]];
    [request setPredicate:predicates];  
    //On Below line, My app frezes and goes into deadlock, this happens randomly while performing
    //some data request using Core data
    NSArray *arrayofrecord=[managedobjectcontext executeFetchRequest:request error:nil];    

    return arrayofrecord;
}

I am trying to attach some screen shots of the stack of calls (These I see when I pause my app) The method with a checkmark in the image,at which deadlock occur is mentioned above The method with a checkmark in the image,at which deadlock occur is mentioned above

Rahul
  • 509
  • 2
  • 4
  • 22

3 Answers3

3

You have to lock the thread. This problem appears when multiple threads access the same piece of code. But be were not to end up in to a dead lock.

static NSString *fetchRequest = @"fetchRequest";
    NSArray *results;
    @synchronized (fetchRequest){
        managedobjectcontext=[Singleton sharedmysingleton].managedobjectcontext;
        NSEntityDescription *entity=[NSEntityDescription entityForName:entityname inManagedObjectContext:managedobjectcontext];

        NSFetchRequest *request=[[NSFetchRequest alloc] init];
       [request setEntity:entity];

       NSPredicate *predicates=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"userName==\"%@\" AND password==\"%@\"",username,password]];
       [request setPredicate:predicates];  
       //On Below line, My app frezes and goes into deadlock, this happens randomly while performing
       //some data request using Core data
       results = [managedobjectcontext executeFetchRequest:request error:nil];    
}
return results;
Alex Terente
  • 12,006
  • 5
  • 51
  • 71
  • Hello @AlexTerente, would you mind taking a look to [this question](http://stackoverflow.com/questions/23504936/core-data-executefetchrequest-freezes-app-in-a-background-thread) and tell me if your solution might also apply there? – mirx May 07 '14 at 07:47
0

As far as I can understand from your dump, you are calling CoreData Context within a different thread other than MainThread.

Keep in mind that CoreData Context are not thread safe and is your responsability to correct use it.

Apple documentation about CoreData and Thread is very exhaustive.

The solutions proposed above are not safe at all: synchronized statement is useless if you are programming in a concurrent environment (ie. you have more than one thread that we assume can access concurrently the same MOC).

You can try to "confine" your context within the thread life-cycle. For example:

dispatch_async(dispatch_get_global_queue(0, 0), ^(){
NSManagedObjectContext* context = [[NSManagedObjectContext alloc] init];
context.persistentStoreCoordinator = self.mainContext.persistentStoreCoordinator;

//Make the fetch and export results to main thread
...
}); 
valvoline
  • 7,737
  • 3
  • 47
  • 52
-1

You can try [private performBlock:^{}]; when using Core Data in multi-thread environment.

For more details, please check this document https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/Concurrency.html#//apple_ref/doc/uid/TP40001075-CH24-SW1

William
  • 191
  • 1
  • 6