让我首先描述场景,然后描述问题:

我创建了一个使用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..."];

}


现在,问题是按钮单击上没有显示第一个通知“ Please wait ...”,但是最后一个通知显示得很好。

我也尝试在单独的线程中调用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(), ^{
        });
    });


非常感谢您的帮助。先感谢您。

最佳答案

问题是您在与代码的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..."];
        });
    });
}

关于objective-c - 如何使用NSUserNotification在单击一次按钮时两次显示用户通知?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31852571/

10-12 03:46
查看更多