从后台线程返回后代码不工作
我有一个表视图(文件名 - Five.m ),它工作正常并正确显示表视图中的数据。当用户选择一行时,它会添加一个子视图,该子视图在同一屏幕上显示进度指示器。添加子视图后,它会启动一个后台线程来解析 HTML 数据并将其添加到 SQLite3 数据库。后台线程、SQLite 数据插入、HTML 解析都工作正常。现在,当它完成时,它会返回到removeProgressInd方法中的 Five.m,该方法会删除进度指示器并导航到新的表视图(文件名 - TitleOnly.m)。它成功返回,因为我可以看到来自removeProgressInd 方法的所有 NSLog 消息。但代码不会停止进度指示器,也不会移动到新的表格视图。代码运行没有任何错误。我尝试在 viewDidLoad 和 viewDidAppear 中运行相同的代码而不是removeProgressInd,但我只看到 NSLog 消息。这是我的代码。
Five.m - tableview DidSelelectRow
{
// start ProgressInd
[self.view addSubview:self.progressInd];
// shows progress Indicator on screen when I run my app in simulator
// start Parsing Calculation in Background thread
if (appDelegate4.globalParsingCompletedFlag==@"0")
{
NSLog(@"starting backgroung thread- parsing calulation- because flag==0");
ParsingCalculation *parsC = [[ParsingCalculation alloc] init];
[queue addOperation:parsC];
[parsC performSelectorInBackground:@selector(main) withObject:nil];
[parsC release];
//successfully starts parsing in background
}
}
ParsingCalculation.m 文件代码
- (void)main {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
//your code to do the operations...
appDelegate5=[[[UIApplication sharedApplication] delegate] retain];
NSLog(@"Now we are in ParsingCalulation.m");
//resultsFromURL is another method in parsingCalulation.m that does all parsing successfully
[self resultsFromURL:appDelegate5.globalLocationURL];
NSLog(@"Now we are moving back in Five.m -calling function removeProgressInd ");
[[Five shared] performSelectorOnMainThread:@selector(removeProgressInd)
withObject:nil
waitUntilDone:YES];
//it returns back to five.m successfully after finishing HTML Parsing
[pool release];
}
Five.m - removeProgressInd 方法
-(void) removeProgressInd{
NSLog(@"Now we are back in Five.m -in function removeProgressInd ");
//remove progressInd from superview as parsing calculation has been completed
[progressInd stopAnimating];
[self.progressInd removeFromSuperview];
//move to TitleOnly.m tableview
NSLog(@"navigate to TitleOnly ");
TitleOnly *newView = [[TitleOnly alloc] initWithNibName:@"TitleOnly" bundle:nil];
[self.navigationController pushViewController:newView animated:YES];
[newView release];
}
我可以在控制台中看到“导航到 TitleOnly”消息,而无需任何错误都意味着它成功运行了 [progressInd stopAnimating] 和 [self.progressInd removeFromSuperview] 命令,没有任何错误,因为这些命令就在此 NSLog 消息之前。但它不会从屏幕上删除进度指示器。同样,它不显示 TitleOnly 表格视图。 ”
我该如何解决它?问题出在哪里?我做错了什么?
请尽快回复。
I have a tableview (file name- five.m ) which works fine and shows data in tableview properly. When a user select a row then it adds a subview which shows a Progress Indicator on the same screen. After adding subview it starts a background thread which parse HTML Data and add it to SQLite3 database. Background thread, SQLIte data insertion, HTML Parsing all are working fine. Now when it finishes then it returns back to five.m in removeProgressInd method which removes progress indicator and navigate to new table view (file name- TitleOnly.m). It returns successfully as I can see all NSLog messages from removeProgressInd method. But code doesn't stop progress Indicator and doesn't move to new tableview. Code runs without any error. I tried to run the same code in viewDidLoad and viewDidAppear instead of removeProgressInd but I see only NSLog Messages. Here is my code.
Five.m - tableview DidSelelectRow
{
// start ProgressInd
[self.view addSubview:self.progressInd];
// shows progress Indicator on screen when I run my app in simulator
// start Parsing Calculation in Background thread
if (appDelegate4.globalParsingCompletedFlag==@"0")
{
NSLog(@"starting backgroung thread- parsing calulation- because flag==0");
ParsingCalculation *parsC = [[ParsingCalculation alloc] init];
[queue addOperation:parsC];
[parsC performSelectorInBackground:@selector(main) withObject:nil];
[parsC release];
//successfully starts parsing in background
}
}
ParsingCalculation.m file code
- (void)main {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
//your code to do the operations...
appDelegate5=[[[UIApplication sharedApplication] delegate] retain];
NSLog(@"Now we are in ParsingCalulation.m");
//resultsFromURL is another method in parsingCalulation.m that does all parsing successfully
[self resultsFromURL:appDelegate5.globalLocationURL];
NSLog(@"Now we are moving back in Five.m -calling function removeProgressInd ");
[[Five shared] performSelectorOnMainThread:@selector(removeProgressInd)
withObject:nil
waitUntilDone:YES];
//it returns back to five.m successfully after finishing HTML Parsing
[pool release];
}
Five.m - removeProgressInd method
-(void) removeProgressInd{
NSLog(@"Now we are back in Five.m -in function removeProgressInd ");
//remove progressInd from superview as parsing calculation has been completed
[progressInd stopAnimating];
[self.progressInd removeFromSuperview];
//move to TitleOnly.m tableview
NSLog(@"navigate to TitleOnly ");
TitleOnly *newView = [[TitleOnly alloc] initWithNibName:@"TitleOnly" bundle:nil];
[self.navigationController pushViewController:newView animated:YES];
[newView release];
}
I can see "navigate to TitleOnly" message in console without any error it means it ran [progressInd stopAnimating] and [self.progressInd removeFromSuperview] commands successfully without any error as these commands are just before this NSLog message. But it doesn't remove progress Indicator from screen. Similarly it doesn't show TitleOnly tableview. ''
How can I fix it? where is the problem? What am I doing wrong?
Please reply as soon as possible.
您是否尝试过使用断点和调试器来单步执行通过问题?与使用 NSLog() 相比,您将更多地了解这种方式所发生的情况。
Have you tried using breakpoints and the debugger to step through the problem? You will learn much more about what is going on this way as opposed to using
NSLog()
's.