zoukankan      html  css  js  c++  java
  • 再看Core Data中PSC陷入死锁的问题

    在《Core Data Programming Guide》文档的Concurrency with Core Data这一章节中提到了“Use Thread Confinement to Support Concurrency”,这里的“Thread”还包含serial operation queue和dispatch queue。

    我目前的理解是MOC会负责对PSC进行加锁解锁,这些动作需要保证有序地得到执行,如果两个thread同时访问一份context,尤其是其中一个进行写操作,另一个进行读操作,就很容易发生死锁:

    - (void)runSave
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
            NSManagedObjectContext *moc = appDelegate.managedObjectContext;
            
            if (moc) {
                for (int j = 0; j < 10000; ++j) {
                    
                    NSString *threadPtr = [NSString stringWithFormat:@"%p", [NSThread currentThread]];
                    [_threadDict setObject:@"Running Thread" forKey:threadPtr];
                    
                    People *people = [NSEntityDescription insertNewObjectForEntityForName:@"People" inManagedObjectContext:moc];
                    people.name = @"noname";
                }
                
                NSLog(@"**********IN SAVE %@", [NSThread currentThread]);
                NSError *error = nil;
                if ([moc save:&error]) {
                    ;
                }
    
                NSLog(@"**********OUT SAVE %@", [NSThread currentThread]);
            }
        });
    }
    
    - (void)runFetch
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
            AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
            NSManagedObjectContext *moc = appDelegate.managedObjectContext;
            
            if (moc) {
                NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
                [fetchRequest setEntity:[NSEntityDescription entityForName:@"People" inManagedObjectContext:moc]];
                
                NSLog(@"~~~~~~~~~~IN FETCH %@", [NSThread currentThread]);
                NSError *error = NULL;
                NSArray *array = [moc executeFetchRequest:fetchRequest error:&error];
                if (array) {
                    ;
                }
                NSLog(@"~~~~~~~~~~OUT FETCH %@", [NSThread currentThread]);
            }
        });
    }

    死锁的表现为:


  • 相关阅读:
    我的.emacs配置文件
    C语言夜未眠5——变量前缀代表的含义
    c语言夜未眠4——对某一位或几位进行反转
    指针也可这么玩:返回局部指针变量,局部噢
    lua学习之table类型
    10个最“优秀”的代码注释
    为什么我希望用C而不是C++来实现ZeroMQ
    c语言夜未眠2——实现撤销和重做
    emacs学习笔记(基本概念)
    flutter json_serializable
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3395196.html
Copyright © 2011-2022 走看看