gpt4 book ai didi

iphone - CoreData 应用程序在执行获取请求时卡住 pthread_mutex_lock

转载 作者:行者123 更新时间:2023-12-03 19:55:00 28 4
gpt4 key购买 nike

我正在使用 Core Data 来管理我的应用程序中的数据库。

我无法在这里发布代码,因为它太长了。但我想我可以用一小行代码和一些快照来解释我的问题。

+(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;
}

我正在尝试附加一些调用堆栈的屏幕截图(这些是我在暂停应用程序时看到的)图中打勾的方法,就是上面提到的发生死锁的方法 The method with a checkmark in the image,at which deadlock occur is mentioned above

最佳答案

您必须锁定线程。当多个线程访问同一段代码时就会出现这个问题。但我们最终不会陷入僵局。

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;

关于iphone - CoreData 应用程序在执行获取请求时卡住 pthread_mutex_lock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11630702/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com