首先让我描述一下这个场景,然后我将描述这个问题:
我创建了一个使用NSUserNotification
显示用户通知的函数 -(void)notify:(NSString*) message {
NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = @"TechHeal";
notification.informativeText = message;
//notification.soundName = NSUserNotificationDefaultSoundName;
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
}
我有一个按钮从服务器获取详细信息。在按钮点击的最后和结尾处,我已调用通知,如下所示:
-(IBAction)get2000Rows:(id)sender{
[self notify:@"Please wait..."];
//some code that takes a while to run. like 10 minues :P
[self notify:@"Thanks for waiting..."];
}
现在,问题是第一个通知“请稍候......”没有显示在按钮点击上,但最后一个通知显示完好。
我也尝试在一个单独的线程中调用Notify函数,但它没有用。 (如下所示)
dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0);
dispatch_async(backgroundQueue, ^{
[self notify:@"Please wait..."];
dispatch_async(dispatch_get_main_queue(), ^{
});
});
非常感谢您的帮助。提前谢谢。
答案 0 :(得分:1)
问题是您在与10 minutes
部分代码相同的线程上运行UI
代码。所以你应该将那些使用它分开:
-(IBAction)get2000Rows:(id)sender{
[self notify:@"Please wait..."];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//some code that takes a while to run. like 10 minues :P
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self notify:@"Thanks for waiting..."];
});
});
}
答案 1 :(得分:0)
您可以通过NSTimer
启动计时器以获取重新消息
@interface MyClass ()
{
NSTimer *timer;
}
-(IBAction)get2000Rows:(id)sender{
[self notify:@"Please wait..."];
timer = [NSTimer scheduledTimerWithTimeInterval:10.0f
target:self
selector:@selector(timerClick:)
userInfo:nil
repeats:YES];
}
- (void)timerClick:(NSTimer *)timer {
[self notify:@"Thanks for waiting..."];
}