gpt4 book ai didi

iphone - 核心数据 : inserting Objects crashed in global queue [ARC - iPhone simulator 6. 1]

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:09:52 25 4
gpt4 key购买 nike

我有一个非常简单的 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 次……它崩溃了

我想不通为什么...感谢您的帮助。

enter image description here

enter image description here

最佳答案

核心数据应该始终在有 moc 的线程上工作。performBlockperformBlockAndWait 的唯一工作是负责线程安全。通过它插入核心数据将始终在正确的线程中运行。您可以在任何线程上定义 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com