想要做一個如下圖的需求 用戶第一次注冊之后顯示
我的思路是:1.在用戶注冊成功的block 里面發(fā)送通知 ?2.在初始化頁面注冊通知監(jiān)聽
[RequestUtilscommonPostMultipartFormDataUtils:self.parmsForAPIbURL:urlbAnimatedViewController:selfconstructingBodyWithBlock:^(id_NonnullformData) {
[MBProgressHUDshowHUDAddedTo:self.viewanimated:YES];
[formDataappendPartWithFileData:UIImageJPEGRepresentation(self.avatarImageView.image,0.5)name:@"photo"fileName:fileNamemimeType:mimeType];
}success:^(id_Nullabledict) {
[MBProgressHUDhideHUDForView:self.viewanimated:YES];
[selfsaveUserDefaultFormRequestAPI:dict];
NSLog(@"rongyun token Test%@",dict);
//發(fā)送紅包通知
[[NSNotificationCenterdefaultCenter]postNotificationName:@"FIRSTREGISTE"object:nil];
AppDelegate*appDelegate = (AppDelegate*)[[UIApplicationsharedApplication]delegate];
[self.viewremoveFromSuperview];
[appDelegateloginSuccess];
}failure:^(id_Nullablefailure) {
[MBProgressHUDhideHUDForView:self.viewanimated:YES];
}];
-(void)viewWillAppear:(BOOL)animated{
[superviewWillAppear:animated];
//add紅包通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(handleFirstRedPacket)name:@"FIRSTREGISTE"object:nil];
}
-(void)viewDidDisappear:(BOOL)animated{
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:@"FIRSTREGISTE"object:nil];
}
但是這樣是不能夠接收到通知的!!J洹E显!0夜俊孽查!因為不在一個線程里面聂受,注冊成功block 里買呢發(fā)送通知 那是一個新的線程蒿秦,而接受卻是在主線程里面,所以說發(fā)送和接受必須在同一個線程蛋济,而且最好都在主線程棍鳖,利用gcd 來從子線程回到主線程,所以改代碼如下:
[RequestUtilscommonPostMultipartFormDataUtils:self.parmsForAPIbURL:urlbAnimatedViewController:selfconstructingBodyWithBlock:^(id_NonnullformData) {
[MBProgressHUDshowHUDAddedTo:self.viewanimated:YES];
[formDataappendPartWithFileData:UIImageJPEGRepresentation(self.avatarImageView.image,0.5)name:@"photo"fileName:fileNamemimeType:mimeType];
}success:^(id_Nullabledict) {
[MBProgressHUDhideHUDForView:self.viewanimated:YES];
[selfsaveUserDefaultFormRequestAPI:dict];
NSLog(@"rongyun token Test%@",dict);
dispatch_async(dispatch_get_main_queue(), ^{
//發(fā)送紅包通知
[[NSNotificationCenterdefaultCenter]postNotificationName:@"FIRSTREGISTE"object:nil];
});
AppDelegate*appDelegate = (AppDelegate*)[[UIApplicationsharedApplication]delegate];
[self.viewremoveFromSuperview];
[appDelegateloginSuccess];
}failure:^(id_Nullablefailure) {
[MBProgressHUDhideHUDForView:self.viewanimated:YES];
}];