我的应用程序有3个屏幕。首先是登录。其次是搜索和第三个是处理任务。 登录时,我从Web服务中检索数据。它以XML格式返回数据。所以数据相当大。所以,我做在后台线程像这样的任务停止Mainthread冻结了我的电话:处理iOS中的两个屏幕和一个活动指示器
-(BOOL)loginEmp
{
.....some computation
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
(unsigned long)NULL), ^(void) {
[self getAllCustomerValues];
});
}
-(void)getAllCustomerValues
{
....more computation.Bring the data,parse it and save it to CoreData DB.
//notification - EDIT
NSNotification *notification =[NSNotification notificationWithName:@"reloadRequest"
object:self];
[[NSNotificationCenter defaultCenter] postNotification : notification];
}
//EDIT
//SearchScreenVC.m
- (void)viewDidLoad
{
....some computation
[self.customerActIndicator startAnimating];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(stopActivityIndicator)
name:@"reloadRequest"
object:nil];
}
- (void)stopActivityIndicator
{
[self.customerActIndicator stopAnimating];
self.customerActIndicator.hidesWhenStopped = YES;
self.customerActIndicator.hidden =YES;
NSLog(@"HIt this at 127");
}
这样的条件下,登录成功,我移动到屏幕2.但后台线程仍在进行中(我知道,因为我有日志记录值)。我想在这里显示一个活动指示器(第二个屏幕),告诉用户在开始搜索之前等待。那么我该怎么做呢?我如何让我的活动指示器监听/等待后台线程。请让我知道如果你需要更多的info.Thanks
编辑:所以,我按编辑,但该通知不会被调用。我在getAllCustomerValues的结尾处放了一个通知,并且在我使用过的SearchScreen的viewDidLoad中。第二个屏幕上停止动画的通知永远不会被调用。什么是我是doing.?Thanks
EDIT 2的错误:所以最后打的方法。我不知道是什么让它达到这种方法。我放了一个断点。我写信停止动画,但它不会。我写了隐藏,并且两者都隐藏到YES。但它仍然保持动画。如何让它停止?
您正在记录值,因此大概您知道何时解析完成。通常情况下,我会在该点使用'NSNotification'来让UI知道它可以允许搜索。 – livingtech
在getAllCustomerValues进程结束时,调用主线程上的一个方法来指示进程已完成。通过发出通知或设置标志。通知将是你最好的选择。 – Bergasms
@livingtech。我添加了通知代码。你可以看看它,告诉我那里有什么错误。 – RookieAppler