CoreData多线程安全
来源:互联网 发布:销售出入库软件 编辑:程序博客网 时间:2024/06/05 08:21
CoreData中的NSManagedObjectContext在多线程中不安全,如果想要多线程访问CoreData的话,最好的方法是一个线程一个NSManagedObjectContext,
,每个NSManagedObjectContext对象实例都可以使用同一个NSPersistentStoreCoordinator实例,这个实例可以很安全的顺序访问永久存储,这是因为NSManagedObjectContext会在便用NSPersistentStoreCoordinator前上锁。
ios5.0为NSManagedObjectContext提供了initWithConcurrentcyType方法,其中的一个NSPrivateQueueConcurrencyType,会自动的创建一个新线程来存放NSManagedObjectContext而且它还会自动创建NSPersistentStoreCoordinator,AppDelegate和前一章的一样,ios5.0之前的可以用GCD来实现
- (IBAction)addIntoDataSource:(id)sender {// User* user=(User *)[NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:self.myAppDelegate.managedObjectContext];// [user setName:_nameText.text];// [user setAge:[NSNumber numberWithInteger:[_ageText.text integerValue]]];// [user setSex:_sexText.text];//// Address* address=(Address *)[NSEntityDescription insertNewObjectForEntityForName:@"Address" inManagedObjectContext:self.myAppDelegate.managedObjectContext];// [address setIdentify:[NSNumber numberWithInteger:[_identyText.text integerValue]]];// [address setHomelocation:@"咸宁"];// NSError* error;// BOOL isSaveSuccess=[_myAppDelegate.managedObjectContext save:&error];// if (!isSaveSuccess) {// NSLog(@"Error:%@",error);// }else{// NSLog(@"Save successful!");// } NSManagedObjectContext* context=[[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];// context.parentContext=_myAppDelegate.managedObjectContext; [context performBlock:^{ //background thread [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mocDidSaveNotification:) name:NSManagedObjectContextDidSaveNotification object:nil]; User* user=(User *)[NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:self.myAppDelegate.managedObjectContext]; [user setName:_nameText.text]; [user setAge:[NSNumber numberWithInteger:[_ageText.text integerValue]]]; [user setSex:_sexText.text]; NSError* error; if (![context save:&error]) { NSLog(@"Error is %@",error); }// //main thread// [_myAppDelegate.managedObjectContext performBlock:^{// NSError* error;// if (![_myAppDelegate.managedObjectContext save:&error]) {// NSLog(@"error is %@",error);// }// // }]; }]; // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{// // dispatch_sync(dispatch_get_main_queue(), ^{// // });// });}通知中心
-(void)mocDidSaveNotification:(NSNotification *)notification{ NSManagedObjectContext* saveContext=[notification object]; if (_myAppDelegate.managedObjectContext==saveContext) { return; } if (_myAppDelegate.managedObjectContext.persistentStoreCoordinator!=saveContext.persistentStoreCoordinator) { return; } dispatch_sync(dispatch_get_main_queue(), ^{ [_myAppDelegate.managedObjectContext mergeChangesFromContextDidSaveNotification:notification]; });}
其实也可以不用通知就是把 下面的内容不让其注释,同时注释通知中心就行了
// context.parentContext=_myAppDelegate.managedObjectContext;
// //main thread
// [_myAppDelegate.managedObjectContext performBlock:^{
// NSError* error;
// if (![_myAppDelegate.managedObjectContext save:&error]) {
// NSLog(@"error is %@",error);
// }
//
// }];
有什么问题请多多指教
- CoreData多线程安全
- CoreData多线程安全
- CoreData多线程安全
- CoreData多线程安全
- CoreData多线程安全
- CoreData多线程安全
- CoreData多线程安全
- [IOS 开发] CoreData多线程安全
- Coredata多线程
- CoreData线程安全
- iOS代码重构(二)CoreData多线程(支持线程安全)
- 多线程操作数据库 (CoreData)
- 多线程操作数据库 (CoreData)
- coreData 多线程同步
- coredata 多线程操作
- 多线程操作数据库 (CoreData)
- CoreData 多线程使用方法
- 认识CoreData-多线程
- sicily 1001. Alphacode
- Unbound classpath container: 'JRE System Library [jdk1.6.0_04]' in project
- vector顺序容器的添加与删除
- 洗牌和发牌游戏
- AS3优化技巧总结(不包括数据库和网络)
- CoreData多线程安全
- JSP 中使用<%@include%> 报 Duplicate local variable path 错误的解决方法
- java 泛型
- 在ProgressBar上加文字----显示百分比的进度条
- mysql 触发器
- 敏捷开发过程剖析及工具推荐
- php与文件操作
- LaTeX入门系列之三(LaTeX经典电子书)
- Linux命令使用