Authored by 郁健超

修改弹幕相关问题 review by阿瑟

@@ -11,7 +11,7 @@ @@ -11,7 +11,7 @@
11 #import "YH_BarrageMsgCell.h" 11 #import "YH_BarrageMsgCell.h"
12 #import "YH_BarrageJoinCell.h" 12 #import "YH_BarrageJoinCell.h"
13 #import "UtilsMacros.h" 13 #import "UtilsMacros.h"
14 - 14 +#import <pthread.h>
15 #define kMessageCellID @"MessageCelliden" 15 #define kMessageCellID @"MessageCelliden"
16 #define kJoinCellID @"JoinCellIden" 16 #define kJoinCellID @"JoinCellIden"
17 #define MaxMessageCacheCapacity 20 17 #define MaxMessageCacheCapacity 20
@@ -142,7 +142,8 @@ @@ -142,7 +142,8 @@
142 142
143 - (void)newUserJoin:(NSDictionary *)item { 143 - (void)newUserJoin:(NSDictionary *)item {
144 __weak typeof (self) weakSelf = self; 144 __weak typeof (self) weakSelf = self;
145 - dispatch_sync(dispatch_get_main_queue(), ^{ 145 + if (pthread_main_np() != 0) {
  146 + // main thread
146 [weakSelf caculateContentInset]; 147 [weakSelf caculateContentInset];
147 YH_BarrageMsgModel *newMessage = [[YH_BarrageMsgModel alloc]initWithDictionary:item error:nil]; 148 YH_BarrageMsgModel *newMessage = [[YH_BarrageMsgModel alloc]initWithDictionary:item error:nil];
148 YH_BarrageMsgModel *oldMessage = [weakSelf.dataMutableArray lastObject]; 149 YH_BarrageMsgModel *oldMessage = [weakSelf.dataMutableArray lastObject];
@@ -156,10 +157,24 @@ @@ -156,10 +157,24 @@
156 [weakSelf reloadData]; 157 [weakSelf reloadData];
157 CGPoint bottomOffset = CGPointMake(0, weakSelf.contentSize.height - weakSelf.bounds.size.height); 158 CGPoint bottomOffset = CGPointMake(0, weakSelf.contentSize.height - weakSelf.bounds.size.height);
158 [weakSelf setContentOffset:bottomOffset animated:YES]; 159 [weakSelf setContentOffset:bottomOffset animated:YES];
  160 + }else {
  161 + dispatch_sync(dispatch_get_main_queue(), ^{
  162 + [weakSelf caculateContentInset];
  163 + YH_BarrageMsgModel *newMessage = [[YH_BarrageMsgModel alloc]initWithDictionary:item error:nil];
  164 + YH_BarrageMsgModel *oldMessage = [weakSelf.dataMutableArray lastObject];
159 165
  166 + if ([weakSelf.dataMutableArray count] && oldMessage && oldMessage.cmd == newMessage.cmd && oldMessage.cmd == 4) {
  167 + [weakSelf.dataMutableArray replaceObjectAtIndex:[weakSelf.dataMutableArray count]-1 withObject:newMessage];
  168 + } else {
  169 + [weakSelf.dataMutableArray addObject:newMessage];
  170 + }
160 171
161 - 172 + [weakSelf reloadData];
  173 + CGPoint bottomOffset = CGPointMake(0, weakSelf.contentSize.height - weakSelf.bounds.size.height);
  174 + [weakSelf setContentOffset:bottomOffset animated:YES];
162 }); 175 });
  176 + }
  177 +
163 } 178 }
164 // 将缓冲区中的数据存入tableview的数据源 179 // 将缓冲区中的数据存入tableview的数据源
165 - (void)appendCacheToDataArray { 180 - (void)appendCacheToDataArray {
@@ -189,6 +204,15 @@ @@ -189,6 +204,15 @@
189 // 如果返回的数据是本机用户所发,则立即显示,不需要走缓冲策略,直接加入到tableview数据源,刷新列表 204 // 如果返回的数据是本机用户所发,则立即显示,不需要走缓冲策略,直接加入到tableview数据源,刷新列表
190 - (void)greenChannel:(NSDictionary *)item { 205 - (void)greenChannel:(NSDictionary *)item {
191 __weak typeof (self) weakSelf = self; 206 __weak typeof (self) weakSelf = self;
  207 + if (pthread_main_np() != 0) {
  208 + // main thread
  209 + [weakSelf caculateContentInset];
  210 + YH_BarrageMsgModel *message = [[YH_BarrageMsgModel alloc]initWithDictionary:item error:nil];
  211 + [weakSelf.dataMutableArray addObject:message];
  212 + [weakSelf reloadData];
  213 + CGPoint bottomOffset = CGPointMake(0, weakSelf.contentSize.height - weakSelf.bounds.size.height);
  214 + [weakSelf setContentOffset:bottomOffset animated:YES];
  215 + }else {
192 dispatch_sync(dispatch_get_main_queue(), ^{ 216 dispatch_sync(dispatch_get_main_queue(), ^{
193 [weakSelf caculateContentInset]; 217 [weakSelf caculateContentInset];
194 YH_BarrageMsgModel *message = [[YH_BarrageMsgModel alloc]initWithDictionary:item error:nil]; 218 YH_BarrageMsgModel *message = [[YH_BarrageMsgModel alloc]initWithDictionary:item error:nil];
@@ -197,7 +221,7 @@ @@ -197,7 +221,7 @@
197 CGPoint bottomOffset = CGPointMake(0, weakSelf.contentSize.height - weakSelf.bounds.size.height); 221 CGPoint bottomOffset = CGPointMake(0, weakSelf.contentSize.height - weakSelf.bounds.size.height);
198 [weakSelf setContentOffset:bottomOffset animated:YES]; 222 [weakSelf setContentOffset:bottomOffset animated:YES];
199 }); 223 });
200 - 224 + }
201 225
202 } 226 }
203 227