CoreData应用程序在执行获取请求时冻结请求pthread_mutex_lock [英] CoreData Application Freezes while performing a fetch Request pthread_mutex_lock
本文介绍了CoreData应用程序在执行获取请求时冻结请求pthread_mutex_lock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Core Data来管理我的应用程式中的资料库。
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;
}
我试图附加一些屏幕截图看到我暂停我的应用程序)
在图像中出现死锁的方法如上所述
推荐答案
你必须锁定线程。当多个线程访问同一段代码时,会出现此问题。但是不要最终进入死锁。
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;
这篇关于CoreData应用程序在执行获取请求时冻结请求pthread_mutex_lock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文