当我尝试保存私有NSManagedObjectContext
的数据时,即使在performBlock:
块内调用此上下文的操作,我也会遇到死锁。这很奇怪,因为这个块应该在不中断主 UI / UX 线程的情况下异步执行。
以下是此方法的代码:
- (void)loadTimetableToCoreData:(id)timetable
{
// Initializing temporary context
NSManagedObjectContext *tempContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
tempContext.parentContext = self.moc;
[tempContext performBlock:^{
// Here is the parsing
NSLog(@"Finished loading to temp MOC");
NSFetchRequest* r = [NSFetchRequest fetchRequestWithEntityName:@"Timetable"];
[r setIncludesPendingChanges:NO];
NSArray *existingTimetables = [tempContext executeFetchRequest:r error:nil];
for (Timetable *table in existingTimetables) {
[tempContext deleteObject:table];
}
// Saving procedure with multithreading
NSError *error;
if (![tempContext save:&error]) {
NSLog(@"Couldn't save: %@", [error localizedDescription]);
}
NSLog(@"Finished saving to temp MOC");
[self.moc performBlock:^{
// Save groups to presistant store
NSError *error;
if (![self.moc save:&error]) {
NSLog(@"Couldn't save: %@", [error localizedDescription]);
}
NSLog(@"Finished saving to main MOC");
[self.writer performBlock:^{
// Save groups to presistant store
NSError *error;
if (![self.writer save:&error]) {
NSLog(@"Couldn't save: %@", [error localizedDescription]);
}
NSLog(@"Finished saving to writer MOC");
}];
}];
}];
}
正如你所看到的,这个方法中有一些NSLog
并且可视化我的问题我遇到了已完成加载到临时MOC 和已完成保存到临时MOC < / strong>即可。
我做错了什么以及如何解锁主线程?
以下是乐器的屏幕以及我可以看到的内容...... controllerDidChangeCintent
正在扼杀所有内容。
我知道这是由loadTimetableToCoreData:
NSFetchRequest* r = [NSFetchRequest fetchRequestWithEntityName:@"Timetable"];
[r setIncludesPendingChanges:NO];
NSArray *existingTimetables = [tempContext executeFetchRequest:r error:nil];
for (Timetable *table in existingTimetables) {
[tempContext deleteObject:table];
}
但我无法摆脱他们!或者我不知道如何......