- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个非常简单的 Core Data 演示,其中只有一个按钮。
当我点击“运行”按钮时,应用程序在 for 循环中创建了 10,000 个对象,该对象在全局队列中运行。
更新更多细节:如果我将 for 循环放在主线程中,它运行良好。
更新我的意图:我知道 MOC 不是线程安全的,但根据 the Apple doc ,我们也可以使用串行队列访问MOC,串行队列使用多个线程。
这里我创建了核心数据栈:
#pragma mark - Core Data Stack
- (NSManagedObjectContext *)managedObjectContext
{
if (nil != _managedObjectContext) {
return _managedObjectContext;
}
_managedObjectContext = [[NSManagedObjectContext alloc] init];
if (self.persistentStoreCoordinator) {
[_managedObjectContext setPersistentStoreCoordinator:self.persistentStoreCoordinator];
}
return _managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel
{
if (nil != _managedObjectModel) {
return _managedObjectModel;
}
_managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (nil != _persistentStoreCoordinator) {
return _persistentStoreCoordinator;
}
NSString *storeType = NSSQLiteStoreType;
NSString *storeName = @"model.sqlite";
NSURL *storeURL = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:storeName]];
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
NSError *error = nil;
if (![_persistentStoreCoordinator addPersistentStoreWithType:storeType
configuration:nil
URL:storeURL
options:nil
error:&error])
{
NSLog(@"Error : %@\n", [error localizedDescription]);
NSAssert1(YES, @"Failed to create store %@ with NSSQLiteStoreType", [storeURL path]);
}
return _persistentStoreCoordinator;
}
#pragma mark -
#pragma mark Application's Documents Directory
- (NSString *)applicationDocumentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
应用启动后:
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if (self.managedObjectContext) {
;
}
return YES;
}
当我点击按钮时:
- (IBAction)runButtonDidClick:(id)sender
{
/**
* Access the moc using different threads to make deadlock.
*/
[self runSave];
}
- (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) {
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]);
}
});
}
点击运行按钮几次,可能是 2 次或 3 次或 4 次……它崩溃了
我想不通为什么...感谢您的帮助。
最佳答案
核心数据应该始终在有 moc 的线程上工作。performBlock
和performBlockAndWait
的唯一工作是负责线程安全。通过它插入核心数据将始终在正确的线程中运行。您可以在任何线程上定义 moc - performBlock
始终选择正确的线程。
所以:
[self.managedObjectContext performBlock:^{
for(NSDictionary *dic in arr) {
//inserting here!
}
}];
在你的情况下:
- (void)runSave
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *moc = appDelegate.managedObjectContext;
if (moc) {
[moc performBlock:^{
for (int j = 0; j < 10000; ++j) {
People *people = [NSEntityDescription insertNewObjectForEntityForName:@"People" inManagedObjectContext:moc];
people.name = @"noname";
}
NSError *error = nil;
if ([moc save:&error]) {
;
}
}];
}
});
}
关于iphone - 核心数据 : inserting Objects crashed in global queue [ARC - iPhone simulator 6. 1],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16958662/
我是 Arduino 世界的新手。编写了以下代码 - 使用 DHT22 传感器。 12 小时后,当温度低于所需温度时,我的温室的加热器没有打开。 关闭电源并重新打开后,一切恢复正常。我知道这段代码很乱
我正在使用 CENTOS6 和 REDHAWK2.0 (RH2.0)。基于“VirtualBox 5.0.16 for Windows host”和“VirtualBox 5.0.17 r106140
在无渲染模式下使用 VMR-9 时如何解决 SelectAtPosition() 崩溃? 最佳答案 使用无渲染模式时的 SelectAtPosition() 崩溃问题可以通过在自定义分配器中实现 IV
这是我从 logcat 复制的错误。我不知道为什么在应用设置屏幕中清除数据后应用程序崩溃。 02-20 17:22:29.980 839-1094/? I/InputReader: Apps
我收到了来自 Apple 的应用提交反馈: Thank you for your resubmission. We were unable to review your app as it crash
我有一个 iPad 应用程序。使用 Fabric 收集崩溃信息。最近出现了一些像 webthread EXC_BREAKPOINT 这样的崩溃。跟踪堆栈上没有我的代码。所以我不知道为什么会发生崩溃。
我有一个在 ubuntu(EC2 实例,t2.small)上运行的简单 python web-scraper,到目前为止它只打印出一个 url 列表: from bs4 import Beautifu
注意:我的问题之前已关闭,我尝试了此处提到的解决方案 - unknown error: session deleted because of page crash from unknown error
我正在尝试从一个场景过渡到另一个场景,但是当我调用 presentScene 时发生崩溃!场景未存储在类中或被引用,它们直接加载到 presentScene 调用中。 Xcode 崩溃截图: 我的简单
我有点困惑,我的应用程序几乎没有崩溃,崩溃日志也有以下信息。 崩溃:com.twitter.crashlytics.ios.exception 这是否意味着它是 Fabric 库的崩溃还是有效的崩溃?
我尝试在我的 ubuntu 16.04 远程服务器上使用 selenium webdriver 和 python 3.5.2 连接到网页 from pyvirtualdisplay import Di
我正在使用使用 Python 和 Selenium 的 InstaPy。我按 Cron 启动脚本,有时它会崩溃。所以它真的很不规则,有时它会很好地贯穿始终。我也已经在 GitHub Repo 上发帖,
最近我换了电脑,从那以后我无法使用 selenium 启动 chrome。我也尝试过 Firefox,但浏览器实例无法启动。 from selenium import webdriver d = we
我正在使用 InstaPy,它使用 Python 和 Selenium。我按照 Cron 启动脚本,但有时它会崩溃。所以它确实不规则,有时它贯穿得很好。我已经在 GitHub Repo 上发布了,但没
我正在使用 InstaPy,它使用 Python 和 Selenium。我按照 Cron 启动脚本,但有时它会崩溃。所以它确实不规则,有时它贯穿得很好。我已经在 GitHub Repo 上发布了,但没
我正在使用使用 Python 和 Selenium 的 InstaPy。我按 Cron 启动脚本,有时它会崩溃。所以它真的很不规则,有时它会很好地贯穿始终。我也已经在 GitHub Repo 上发帖,
我正在尝试使用rook在kubernetes集群上配置ceph,我已经运行了以下命令: kubectl apply -f common.yaml kubectl apply -f operator.y
我有一个屏幕A,在执行了一些POST API任务后,我启用了一个按钮,然后单击按钮导航到屏幕B。当Reaction Native应用程序冻结并崩溃时,崩溃会随机发生。从其他屏幕导航到屏幕B也不是问题,
我想知道操作系统是否可能导致崩溃,例如我是否有代码,它是否有可能在Windows环境下崩溃而在Linux上却没有崩溃?还是这不可能? 最佳答案 最简单的示例是代码,您的意图是使其与平台兼容,但是您的代
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
我是一名优秀的程序员,十分优秀!