postNotificationName

1.定义消息创建的关联值 也就是找到方法的标志

NSString *const GameToIPhoneNotification = @"GameToIPhoneNotification"; GameToIPhoneNotification变量,@"GameToIPhoneNotification"这个值存于通知中心中,信息中心通过这个值来识别变量

1.注册一个消息中心

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

[center addObserver:self selector:@selector(onToIphone:) name:GameToIPhoneNotification object:nil];

 

-(void)onToIphone:(NSNotification*)notify :这个方法是接受到GameToIPhoneNotification这个通知所调用的方法

2.调用信息

NSNotificationCenter * center = [NSNotificationCenter defaultCenter];

[center postNotificationName:GameToIPhoneNotification object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:SMSRecommendNotification] , @"actcode",nil]];

 

[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:SMSRecommendNotification] 这个是传递给-(void)onToIphone:(NSNotification*)notify 的参数。



项目的聊天窗口遇到一个白屏的问题:

只要发送一个图片后,再发任何消息都导致聊天窗口白屏。

出问题的代码摘要如下:

//HWChatViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
       
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadChatCollectionViewData) name:HWCHAT_VIEW_NEED_RELOAD object:nil];
}

-(void)reloadChatCollectionViewData {
    HWLog(@"111111*********");
    yoho_dispatch_execute_in_main_queue(^{
        HWLog(@"22222==========");
        [_collectionView reloadData];
        [_collectionView layoutIfNeeded];
        HWLog(@"33333+++++++++++");
        [_collectionView scrollToBottomAnimated:YES];
    });
}


 
  

//HWChatManager.m
-(void)reloadChatCollectionViewData {
    yoho_dispatch_execute_in_main_queue(^{
        HWLog(@"抛通知HWCHAT_VIEW_NEED_RELOAD");
        [[NSNotificationCenter defaultCenter] postNotificationName:HWCHAT_VIEW_NEED_RELOAD object:nil];
    });
}



//HWPhotoMediaItem

- (UIView *)mediaView {
    if (_cachedImageView == nil) {
        CGSize size = [self mediaViewDisplaySize];
        UIImageView *imageView = [[UIImageView alloc] init];
        
        NSURL *url = [NSURL URLWithString:_imageUrl];
        if ([[url.scheme lowercaseString] isEqualToString:@"http"]) {
            [imageView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                _size = image.size;
                yoho_dispatch_execute_in_main_queue(^{
                    HWLog(@"抛通知HWCHAT_VIEW_NEED_RELOAD");
                    [[NSNotificationCenter defaultCenter] postNotificationName:HWCHAT_VIEW_NEED_RELOAD object:nil];
                });
            }];
        } else {
             //other code
        }

        //other code
    }
    
    return _cachedImageView;
}



聊天窗口发图,输出的log如下:


|[HWChatManager.m 175]: __45-[HWChatManager reloadChatCollectionViewData]_block_invoke 抛通知HWCHAT_VIEW_NEED_RELOAD
|[HWChatViewController.m 162]: -[HWChatViewController reloadChatCollectionViewData] 111111*********
|[HWChatViewController.m 164]: __52-[HWChatViewController reloadChatCollectionViewData]_block_invoke 22222==========
|[HWPhotoMediaItem.m 41]: __29-[HWPhotoMediaItem mediaView]_block_invoke_2 抛通知HWCHAT_VIEW_NEED_RELOAD
|[HWChatViewController.m 162]: -[HWChatViewController reloadChatCollectionViewData] 111111*********
|[HWChatViewController.m 164]: __52-[HWChatViewController reloadChatCollectionViewData]_block_invoke 22222==========
|[HWChatViewController.m 167]: __52-[HWChatViewController reloadChatCollectionViewData]_block_invoke 33333+++++++++++
|[HWChatViewController.m 167]: __52-[HWChatViewController reloadChatCollectionViewData]_block_invoke 33333+


从log可以看出HWChatManager抛通知HWCHAT_VIEW_NEED_RELOAD使得[HWChatViewController reloadChatCollectionViewData]被调用。
[_collectionView reloadData];因为要绘制HWPhotoMediaItem,if(_cachedImageView == nil)时又抛了一个同样的通知,再次同步调用了[HWChatViewController reloadChatCollectionViewData]。
于是[_collectionView reloadData];未完成的情况下又reloadData了一次,图片是发出去且正常显示了,但导致了界面绘制错乱,后续发消息都白屏了(为什么reloadData过程中再reloadData会错乱导致白屏,这个问题待研究).


避免错乱的解决办法是mediaView里抛通知时在异步线程抛,利用线程切换避免reloadData过程中再reloadData。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值