CoreData Application Freezes while performing a fetch Request pthread_mutex_lock

自古美人都是妖i 提交于 2020-01-13 05:41:29

问题


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


回答1:


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;



回答2:


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
...
}); 



回答3:


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



来源:https://stackoverflow.com/questions/11630702/coredata-application-freezes-while-performing-a-fetch-request-pthread-mutex-lock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!