美文网首页
iOS NSOperationQueue mainQueue 妙

iOS NSOperationQueue mainQueue 妙

作者: 传说中的汽水枪 | 来源:发表于2019-12-13 20:06 被阅读0次

    1. VC跳转,收不到通知

    在SecondVC中:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor whiteColor];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(lookupTheInfo:) name:@"kFirstToSecondNotification" object:nil];
    }
    

    在FirstVC中一个点击事件:

    - (void)buttonTouchUpInside:(id)sender {
        RXNotificationSecondViewController *vc = [RXNotificationSecondViewController new];
        [self.navigationController pushViewController:vc animated:YES];
        NSDictionary *userInfo = @{
            @"name":@"Notification",
            @"age":@"18",
            @"height":@"188cm"
        };
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kFirstToSecondNotification" object:nil userInfo:userInfo];
    }
    

    因为SecondVC还没有注册,是无法收到通知。
    所以可以改成:

    - (void)buttonTouchUpInside:(id)sender {
        RXNotificationSecondViewController *vc = [RXNotificationSecondViewController new];
        [self.navigationController pushViewController:vc animated:YES];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            NSDictionary *userInfo = @{
                @"name":@"Notification",
                @"age":@"18",
                @"height":@"188cm"
            };
            [[NSNotificationCenter defaultCenter] postNotificationName:@"kFirstToSecondNotification" object:nil userInfo:userInfo];
        }];
    }
    

    2. VC跳转,SecondVC 需要单独处理一些事情

    - (void)buttonTouchUpInside:(id)sender {
        RXNotificationSecondViewController *secondVC = [RXNotificationSecondViewController new];
        [self.navigationController pushViewController:secondVC animated:YES];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
             [secondVC doSomething];
        }];
    }
    

    实际上,以上也可用如下的方法:

    - (void)method3 {
        RXNotificationSecondViewController *vc = [RXNotificationSecondViewController new];
        [self.navigationController pushViewController:vc animated:YES];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSDictionary *userInfo = @{
                @"name":@"Notification",
                @"age":@"18",
                @"height":@"188cm"
            };
            [[NSNotificationCenter defaultCenter] postNotificationName:@"kFirstToSecondNotification" object:nil userInfo:userInfo];
        });
    }
    

    但是这个是需要估计一个时间,所以不是一个好方法。

    以下的方法是不可以的:

    - (void)method4 {
        RXNotificationSecondViewController *vc = [RXNotificationSecondViewController new];
        [self.navigationController pushViewController:vc animated:YES];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSDictionary *userInfo = @{
                @"name":@"Notification",
                @"age":@"18",
                @"height":@"188cm"
            };
            [[NSNotificationCenter defaultCenter] postNotificationName:@"kFirstToSecondNotification" object:nil userInfo:userInfo];
        });
    }
    

    相关文章

      网友评论

          本文标题:iOS NSOperationQueue mainQueue 妙

          本文链接:https://www.haomeiwen.com/subject/qsrqnctx.html