gpt4 book ai didi

ios - UI 在执行 NSBatchDeleteRequest 时卡住

转载 作者:可可西里 更新时间:2023-11-01 06:00:41 25 4
gpt4 key购买 nike

NSFetchRequest *request = [Report fetchRequest];
request.predicate = [NSPredicate predicateWithFormat: @"reportId IN %@",self.selectedRowsInEditMode];

NSBatchDeleteRequest *deleteDetailsRequest = [[NSBatchDeleteRequest alloc] initWithFetchRequest:request];
deleteDetailsRequest.resultType = NSBatchDeleteResultTypeStatusOnly;
[self.managedObjectContext executeRequest:deleteDetailsRequest error:&errorExecuteRequest];

if (!errorExecuteRequest) {
NSError *deleteSaveError;
BOOL saveSuccessful = [self.managedObjectContext save:&deleteSaveError];
[self.tableView removeLoadingActivityView];

这是我尝试一次删除多个报告托管对象的代码,但是当调用 executeRequest 时,UI 会卡住一段时间然后删除。但是 NSBatchDeleteRequest 在删除方面应该非常快。我尝试了多次修改以使其正常工作但没有成功。我不确定我在这里做错了什么。所以任何人都可以建议让它在没有 UI 故障的情况下工作。

最佳答案

希望对后面的代码有所帮助!!

我最终添加了以下内容以处理批量删除后的上下文。

[self.managedObjectContext refreshAllObjects];

// Initialize Fetch Request
NSFetchRequest *fetchRequest = [Report fetchRequest];

// Add Sort Descriptors
fetchRequest.predicate = [NSPredicate predicateWithFormat: @"reportId IN %@",self.selectedRowsInEditMode];

// Initialize Asynchronous Fetch Request
NSAsynchronousFetchRequest *asynchronousFetchRequest = [[NSAsynchronousFetchRequest alloc] initWithFetchRequest:fetchRequest completionBlock:^(NSAsynchronousFetchResult *result) {
dispatch_async(dispatch_get_main_queue(), ^{
// Process Asynchronous Fetch Result
[weakSelf processAsynchronousFetchResult:result];
});
}];

// Execute Asynchronous Fetch Request
[self.managedObjectContext performBlock:^{
// Execute Asynchronous Fetch Request
NSError *asynchronousFetchRequestError = nil;
NSAsynchronousFetchResult *asynchronousFetchResult = (NSAsynchronousFetchResult *)[weakSelf.managedObjectContext executeRequest:asynchronousFetchRequest error:&asynchronousFetchRequestError];

if (asynchronousFetchRequestError) {
NSLog(@"Unable to execute asynchronous fetch result.");
NSLog(@"%@, %@", asynchronousFetchRequestError, asynchronousFetchRequestError.localizedDescription);
}
}];

或者在下面使用你使用全局队列在后台运行东西和主队列来更新 UI

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Thread
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
});
});

关于ios - UI 在执行 NSBatchDeleteRequest 时卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47516766/

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