Core Data浅谈系列之八 : 关于并发

简介:
有时候,我们需要有个worker thread来做一些密集型或者长耗时的任务,以避免阻塞住UI,给用户不好的体验。比如从网络上获取一批数据,然后解析它们,并将其输出到存储文件中。这时候,由于数据层发生了变动,我们希望通知到主线程更新UI —— 这就涉及到Core Data的多线程特性。

比如我们一直以来使用的Demo中,添加球员信息的AddPlayerViewController和显示球员列表的PlayerListViewController在进行CURD操作时都是在主ViewController的context中完成的,这通过维持一个属性cdViewController指向主ViewController来实现: 
#pragma mark - 
#pragma mark - UITableView Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    Team *teamObject = [self.teamArray objectAtIndex:indexPath.row];
    PlayerListViewController *playerListVC = [[[PlayerListViewController alloc] init] autorelease];
    playerListVC.team = teamObject;
    playerListVC.cdViewController = self;
    [self.navigationController pushViewController:playerListVC animated:YES];
}
以及:
#pragma mark - 
#pragma mark - Player CURD

- (void)addPlayer:(id)sender
{
    AddPlayerViewController *addPlayerVC = [[[AddPlayerViewController alloc] init] autorelease];
    addPlayerVC.cdViewController = self.cdViewController;
    addPlayerVC.team = self.team;
    [self presentModalViewController:addPlayerVC animated:YES];
}
对于比较小的Demo,这么写代码是可以接受的,虽然也会觉得传递得有点长。
当程序的代码规模比较大,或者说处理的数据比较多时,我们可以通过引入并发特性来做一点优化。

通过创建临时的context来添加球员信息: 
- (IBAction)addBtnDidClick:(id)sender
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSManagedObjectContext *tmpContext = [[NSManagedObjectContext alloc] init];
        [tmpContext setPersistentStoreCoordinator:sharedPersistentStoreCoordinator];

        // We don't check the user input.
        Player *playerObject = [NSEntityDescription insertNewObjectForEntityForName:@"Player" inManagedObjectContext:tmpContext];
        playerObject.name = self.nameTextField.text;
        playerObject.age = [NSNumber numberWithInteger:[self.ageTextField.text integerValue]];
        playerObject.team = self.team;

        NSError *error = NULL;
        if (tmpContext && [tmpContext hasChanges] && ![tmpContext save:&error]) {
            NSLog(@"Error %@, %@", error, [error localizedDescription]);
            abort();
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            [self dismissModalViewControllerAnimated:YES];
        });
    });
}
为了响应其它线程的变化, 参考此文档 ,我们可以先监听消息,然后合并发生了的变化:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mocDidSaveNotification:) name:NSManagedObjectContextDidSaveNotification object:nil];


- (void)mocDidSaveNotification:(NSNotification *)notification
{
    NSManagedObjectContext *savedContext = [notification object];

    if (savedContext == self.managedObjectContext) {
        return ;
    }

    if (savedContext.persistentStoreCoordinator != self.persistentStoreCoordinator) {
        return ;
    }

    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"Merge changes from other context.\n");
        [self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
    });
}
这么做了之后,我们尝试添加一名球员,会得到如下错误信息:
2013-01-21 09:56:08.300 cdNBA[573:617] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'team' between objects in different contexts
这是由于我们把主线程context中的team对象传递到临时创建的context中进行操作了。在Core Data的多线程环境中,我们只能传递objectID或者重新fetch:
addPlayerVC.teamID = [self.team objectID];

// ...

playerObject.team = (Team *)[tmpContext objectWithID:self.teamID];
这样可以执行过去,控制台输出:
2013-01-21 10:11:12.834 cdNBA[687:1b03] void _WebThreadLockFromAnyThread(bool), 0x83a91c0: Obtaining the web lock from a thread other than the main thread or the web thread. UIKit should not be called from a secondary thread.
2013-01-21 10:11:12.932 cdNBA[687:c07] Merge changes from other context.
第二行日志说明合并变化了,不过第一行告诉我们在非主线程里面访问了一些UI方面的东西。这是由于上面在global_queue里面访问了UITextField,把访问UI的代码提到外面即可。

BTW,在iOS 5以后,苹果提供了更为便捷有效的parent-child context机制,可以参见 这里

Brief Talk About Core Data Series, Part 8 : About Concurrency 

Jason Lee @ Hangzhou

目录
相关文章
|
开发框架 前端开发 .NET
ASP.NET Core: 十七.Action的执行(Endpoint.RequestDelegate后面的故事)(一)
上一章介绍了经过路由的处理,一个请求找到了具体处理这个请求的EndPoint,并最终执行它的RequestDelegate方法来处理这个Httpcontext。本章继续这个处理进程,按照惯例,依然通过几幅图来聊一聊这个RequestDelegate之后的故事。在此就避免不了的聊到各种Filter,它方便我们在action执行的前后做一些 “小动作”。
101 0
ASP.NET Core: 十七.Action的执行(Endpoint.RequestDelegate后面的故事)(一)
|
中间件 调度 前端开发
使用.NET Core搭建分布式音频效果处理服务(六)让Middleware自动Invoke
  为何要用中间件来实现音频处理的监听服务 当然也可以使用Startup来进行服务的自启动,或者也可以使用quartz定时调度任务来启动音频服务,大家随意。 笔者认为使用中间件的目的,是为了分离应用和服务,也是一种解耦手段。
1016 0
|
安全
「最简单」的 Core Data 上手指南
本文讲的是「最简单」的 Core Data 上手指南,在过去的几个月里,我花费了大量的时间在研究 Core Data 之上,我得去处理一个使用了很多陈旧的代码,糟糕的 Core Data 以及违反了多线程安全的项目。讲真,Core Data 学习起来非常的困难
884 0