gpt4 book ai didi

ios - 调用核心数据操作时崩溃并显示 "failed to scene-update in time"消息

转载 作者:行者123 更新时间:2023-11-29 01:50:41 26 4
gpt4 key购买 nike

我遇到了一个奇怪的问题“未能及时更新场景”。下载数据后,我将其存储到核心数据中。如果应用程序在前台,一切都会正常工作。仅当我在将数据存储到核心数据的过程中连续在状态、后台和前台之间切换应用程序时,才会出现此崩溃。在 Debug模式下不会发生此崩溃。

我尝试将日志放在 Core Data 操作方法的不同阶段,我发现了应用程序实际崩溃的地方。在用户界面中,我使用以下代码

DatabaseManagerClass *database = [[DatabaseManagerClass alloc]init];
[database deleteDataFromFormTable:nil];

[database insertDataInChecklistTable:[self.responseDictionary valueForKey:@"checklists"]];

[self performSelectorOnMainThread:@selector(mainThreadTask) withObject:nil waitUntilDone:YES];`

崩溃发生在“insertDataInChecklistTable”方法内部。我检查了 insertDataInChecklistTable 在其中工作的线程。它仅在主线程中。任何人都可以提出一些建议来解决这个问题。

提前致谢,谢里。

带有执行 block 的更新代码

-(void)insertDataInFormColumnsTable:(NSMutableArray*)responseArray withForm:(Form*)form

{ AppDelegate objAppDelegate = (AppDelegate)[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *moc   = [objAppDelegate managedObjectContext];
NSManagedObjectContext *bgMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];

[bgMOC performBlock:^{
for (int count=0; count<[responseArray count]; count++)
{
NSDictionary *columnDict = [responseArray objectAtIndex:count];

Form_Column *formColumn = [NSEntityDescription insertNewObjectForEntityForName:@"Form_Column" inManagedObjectContext:moc];


if ([columnDict valueForKey:FORMULA]!=[NSNull null]) //Added by mritunjay
formColumn.formula = [columnDict valueForKey:FORMULA];
else
formColumn.formula=nil;

if ([columnDict valueForKey:ASP_EDITABLE]!=[NSNull null])
formColumn.asp_editable = [columnDict valueForKey:ASP_EDITABLE];
else
formColumn.asp_editable=nil;


if ([columnDict valueForKey:CM_EDITABLE]!=[NSNull null])
formColumn.cm_editable = [columnDict valueForKey:CM_EDITABLE];
else
formColumn.cm_editable=nil;

if ([columnDict valueForKey:IM_EDITABLE]!=[NSNull null])
formColumn.im_editable = [columnDict valueForKey:IM_EDITABLE];
else
formColumn.im_editable=nil;


if ([columnDict valueForKey:SP_EDITABLE]!=[NSNull null])
formColumn.sp_editable = [columnDict valueForKey:SP_EDITABLE];
else
formColumn.sp_editable=nil;

if ([columnDict valueForKey:ASP_EDITABLE]!=[NSNull null])
formColumn.dialog_row = [columnDict valueForKey:ASP_EDITABLE];
else
formColumn.dialog_row=nil;

if ([columnDict valueForKey:INDEX]!=[NSNull null])
formColumn.index = [columnDict valueForKey:INDEX];
else
formColumn.index=nil;

if ([columnDict valueForKey:WEB_ONLY]!=[NSNull null])
formColumn.web_only = [columnDict valueForKey:WEB_ONLY];
else
formColumn.web_only=nil;

if ([columnDict valueForKey:LABEL]!=[NSNull null])
formColumn.label = [columnDict valueForKey:LABEL];
else
formColumn.label=nil;

formColumn.form_id = form.form_id;

if ([columnDict valueForKey:ID]!=[NSNull null])
formColumn.column_id=[columnDict valueForKey:ID];
else
formColumn.column_id=nil;

if ([columnDict valueForKey:TYPE]!=[NSNull null])
formColumn.type=[columnDict valueForKey:TYPE];
else
formColumn.type=nil;

NSArray *selectionArray = [columnDict valueForKey:SELECTIONS];

if ([selectionArray count]) {


for (int count = 0; count < [selectionArray count]; count++)
{
Form_Sub_Columns *formSubColumns = [NSEntityDescription insertNewObjectForEntityForName:@"Form_Sub_Columns" inManagedObjectContext:moc];

NSDictionary *subColumnDict = [selectionArray objectAtIndex:count];

if ([subColumnDict valueForKey:ID]!=[NSNull null])
formSubColumns.column_id=[subColumnDict valueForKey:ID];
else
formSubColumns.column_id=nil;

if ([subColumnDict valueForKey:LABEL]!=[NSNull null])
formSubColumns.label=[subColumnDict valueForKey:LABEL];
else
formSubColumns.label=nil;

if ([subColumnDict valueForKey:REQUIRES_REMARKS]!=[NSNull null])
formSubColumns.requires_remarks=[subColumnDict valueForKey:REQUIRES_REMARKS];
else
formSubColumns.requires_remarks=nil;


if ([subColumnDict valueForKey:VALUE]!=[NSNull null])
formSubColumns.value=[subColumnDict valueForKey:VALUE];
else
formSubColumns.value=nil;


if ([subColumnDict valueForKey:DESCRIPTION]!=[NSNull null])
formSubColumns.description_name=[subColumnDict valueForKey:DESCRIPTION];
else
formSubColumns.description_name=nil;



if ([subColumnDict valueForKey:SELECTION_TYPE_ID]!=[NSNull null])
formSubColumns.selection_type_id=[subColumnDict valueForKey:SELECTION_TYPE_ID];
else
formSubColumns.value=nil;

[formColumn addSun_columnsObject:formSubColumns];
}
}
[formColumn addFormObject:form];

[form addColumnsObject:formColumn];

}

if([form.enable_attachment boolValue])
{
Form_Column *formColumn = [NSEntityDescription insertNewObjectForEntityForName:@"Form_Column" inManagedObjectContext:moc];

formColumn.label = @"Add Attachments";

formColumn.form_id = form.form_id;
formColumn.index = [NSNumber numberWithInt:998];
formColumn.column_id=[NSNumber numberWithInt:998]; // hardcoded if Attachment is Enabled.
formColumn.web_only = [NSNumber numberWithInt:0];
formColumn.type=[NSNumber numberWithInt:-1];

[form addColumnsObject:formColumn];

}

if ([[NSThread currentThread] isMainThread]) {
NSLog(@"Main Thread inside block");
}
else {
NSLog(@"Not main thread inside block");
}

NSError *error;

if(![bgMOC save:&error]){
NSLog(@"Error %@", [error localizedDescription]);
}
}];

最佳答案

在主/UI 线程上处理数据总是是个坏主意,会导致这样的问题。

您的 -insertDataIntoChecklistTable 需要在后台队列上运行,这意味着您应该:

  1. 创建第二个 NSManagedObjectContext,它是一个 NSPrivateQueueConcurrencyType
  2. 将其与您的主要背景相关联
  3. 使用 -performBlock: 处理针对 NSManagedObjectContext 的数据
  4. 处理完成后保存

这将使您的数据处理脱离主队列。永远不要处理主队列上的数据。

关于ios - 调用核心数据操作时崩溃并显示 "failed to scene-update in time"消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31452781/

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