【问题标题】:CoreData Application Freezes while performing a fetch Request pthread_mutex_lockCoreData 应用程序在执行获取请求 pthread_mutex_lock 时冻结
【发布时间】:2012-07-22 18:16:28
【问题描述】:

我正在使用 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;
}

我正在尝试附加一些调用堆栈的屏幕截图(这些是我在暂停应用程序时看到的) 上面提到了图片中打勾的方法,发生死锁的方法

【问题讨论】:

    标签: iphone ios multithreading ios5 core-data


    【解决方案1】:

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

    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;
    

    【讨论】:

    • 你好@AlexTerente,你介意看看this question,告诉我你的解决方案是否也适用于那里?
    【解决方案2】:

    据我从您的转储中了解到,您是在 MainThread 以外的不同线程中调用 CoreData Context。

    请记住,CoreData 上下文不是线程安全的,您有责任正确使用它。

    Apple documentation about CoreData and Thread 非常详尽。

    上面提出的解决方案根本不安全:如果您在并发环境中编程(即,我们假设您有多个线程可以同时访问同一个 MOC),同步语句是无用的。

    您可以尝试将您的上下文“限制”在线程生命周期内。例如:

    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】:

      在多线程环境下使用Core Data时可以试试[private performBlock:^{}];

      更多详情请查看此文档https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/Concurrency.html#//apple_ref/doc/uid/TP40001075-CH24-SW1

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多