gpt4 book ai didi

iOS:核心数据 performBlock 使 UI 无响应

转载 作者:行者123 更新时间:2023-11-29 02:46:03 25 4
gpt4 key购买 nike

我只是想弄清楚核心数据私有(private)上下文。我正在尝试使用私有(private)上下文 (NSPrivateQueueConcurrencyType) 插入 20k 记录。但是只要点击“插入 20k 记录”按钮,UI 线程就会挂起。

- (IBAction)insertRecords:(id)sender {


[[CoreDataStore privateContext] performBlock:^{

NSLog(@"Starting to insert 20k records...");
for (int i = 0 ; i < 20000; i++)
{
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:[CoreDataStore privateContext]];
[newManagedObject setValue:[NSDate date] forKey:@"time"];
}
NSError * error = nil;
NSLog(@"Inserted 20k records to managed object context");
[[CoreDataStore privateContext] save:&error];
NSLog(@"Save context command fired");

}];

NSLog(@"returning from insert method");
}

私有(private)上下文的更改正在此处合并到主上下文

- (void)contextDidSavePrivateQueueContext:(NSNotification *)notification
{
@synchronized(self) {

[self.mainContext performBlock:^{
NSLog(@"merging changes to main context.....");
[self.mainContext mergeChangesFromContextDidSaveNotification:notification];
NSLog(@"merged changes to main context");
}];
}
}

下面是我点击插入按钮时的日志窗口输出:

2014-08-04 14:54:12.431 CoreDataDrillDown[11323:90b] returning from insert method
2014-08-04 14:54:12.431 CoreDataDrillDown[11323:1403] Starting to insert 20k records...
2014-08-04 14:54:12.506 CoreDataDrillDown[11323:1403] Inserted 20k records to managed object context
2014-08-04 14:54:12.785 CoreDataDrillDown[11323:90b] merging changes to main context.....
2014-08-04 14:54:12.786 CoreDataDrillDown[11323:1403] Save context command fired
2014-08-04 14:54:27.019 CoreDataDrillDown[11323:90b] merged changes to main context

用户界面超过 15 秒没有响应。我想知道为什么?

使用以下方法建议 https://stackoverflow.com/users/817182/thom-ekhttps://stackoverflow.com/users/2128900/micha%c5%82-ciuba ,插入速度很快,不会卡住 UI,但更改不会保存到磁盘,不知道为什么。

//writerContext has persistent store coordinator, so it should write data to disk but it 
//is not writing any changes to disk.???????
-(NSManagedObjectContext *)writerContext
{
if (!_writerContext)
{
_writerContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
_writerContext.persistentStoreCoordinator = [(AppDelegate *)[[UIApplication sharedApplication] delegate] persistentStoreCoordinator];
}
return _writerContext;
}
//mainContext is being used with NSFetchedResultController
-(NSManagedObjectContext *)mainContext
{
if (!_mainContext)
{
_mainContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
_mainContext.parentContext = [self writerContext];
}
return _mainContext;
}

//performBlock is being called on privateContext
-(NSManagedObjectContext *)privateContext
{
if (!_privateContext)
{
_privateContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
_privateContext.parentContext = [self mainContext];
}
return _privateContext;
}

最佳答案

主要问题是 mainContext 仍然绑定(bind)到 mainQueue,所以它将等待这个巨大的合并。

有关于异步保存的好文章:关于 CocoaneticsObjC.io .

其他解决方案只是重置 mainContext 并重新加载所有 tableView(您不会一次向用户显示所有 20k 条记录)。

关于iOS:核心数据 performBlock 使 UI 无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25115563/

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