Commit c70f9371fce7a4a88b6700b5cc60a3303757aa3a

Authored by daojian
1 parent 6727a581

异步获取群聊列表逻辑调整

CNLiveNewIMAManagerKit.podspec
@@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
8 8
9 Pod::Spec.new do |s| 9 Pod::Spec.new do |s|
10 s.name = 'CNLiveNewIMAManagerKit' 10 s.name = 'CNLiveNewIMAManagerKit'
11 - s.version = '0.5.5' 11 + s.version = '0.5.6'
12 s.summary = 'A short description of CNLiveNewIMAManagerKit.' 12 s.summary = 'A short description of CNLiveNewIMAManagerKit.'
13 13
14 #s.description = <<-DESC 14 #s.description = <<-DESC
CNLiveNewIMAManagerKit/Classes/Playform/IMAContactManager+Group.h
@@ -12,7 +12,7 @@ @@ -12,7 +12,7 @@
12 12
13 @interface IMAContactManager (Group) 13 @interface IMAContactManager (Group)
14 14
15 -/// 异步获取最新群列表 15 +/// 异步获取最新群列表,并缓存群聊类别 和 所有群id,群详情信息
16 - (void)asyncGroupList; 16 - (void)asyncGroupList;
17 17
18 /// 同步获取IM本地缓存的群列表 18 /// 同步获取IM本地缓存的群列表
CNLiveNewIMAManagerKit/Classes/Playform/IMAContactManager+Group.m
@@ -60,18 +60,57 @@ @@ -60,18 +60,57 @@
60 60
61 61
62 62
63 -// 群列表 63 +/// 异步获取最新群列表,并缓存群聊类别 和 所有群id,群详情信息
64 - (void)asyncGroupList 64 - (void)asyncGroupList
65 { 65 {
66 - __weak IMAContactManager *ws = self;  
67 -  
68 - [[TIMGroupManager sharedInstance] getGroupList:^(NSArray *array) {  
69 - [ws createGroupList:array];  
70 - } fail:^(int code, NSString *err) {  
71 - DebugLog(@"Fail:-->code=%d,msg=%@,fun=%s", code, err,__func__); 66 +
  67 + [[TIMGroupManager sharedInstance] getGroupList:^(NSArray<TIMGroupInfoResult *> *allGroupArr) {
  68 +
  69 + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  70 +
  71 + [[CNGroupIDFMDBManager manager] deleteAllGroupId];
  72 +
  73 + /**存放所有群的id*/
  74 + NSMutableArray *groupIdArray = [NSMutableArray array];
  75 +
  76 + for (int i = 0; i < allGroupArr.count; i ++) {
  77 +
  78 + TIMGroupInfo *info = allGroupArr[i];
  79 + [groupIdArray addObject:info.group];
  80 + /**把群id保存到数据库*/
  81 + [[CNGroupIDFMDBManager manager] insertGroupId:info.group];
  82 +
  83 + /**缓存群列表*/
  84 + IMAGroup *group = [[IMAPlatform sharedInstance].contactMgr getUserByGroupId:info.group];
  85 + if (group == nil) {
  86 + [[IMAPlatform sharedInstance].contactMgr onAddGroup:group];
  87 + }
  88 + group.groupInfo.selfInfo = info.selfInfo;
  89 + group.groupInfo.allShutup = info.allShutup;
  90 +
  91 + /**把群免打扰设置保存到数据库*/
  92 + [self saveGroupReceiveMsgStatusWithGroupInfo:info];
  93 + }
  94 +
  95 + /**缓存所有群id*/
  96 + self.groupIdsList = groupIdArray.mutableCopy;
  97 +
  98 + /**刷新会话列表*/
  99 + dispatch_async(dispatch_get_main_queue(), ^{
  100 + [[IMAPlatform sharedInstance].conversationMgr asyncConversationList];
  101 + });
  102 +
  103 + /**同步群聊信息到本地*/
  104 + [self syncGroupInfoWithGroupIdArray:groupIdArray];
  105 + });
  106 +
  107 +
  108 + } fail:^(int code, NSString *msg) {
  109 +
72 }]; 110 }];
73 } 111 }
74 112
  113 +/// 同步获取IM本地缓存的群列表
75 - (void)syncGroupList 114 - (void)syncGroupList
76 { 115 {
77 if (self.groupIdsList == nil) { 116 if (self.groupIdsList == nil) {
@@ -81,179 +120,152 @@ @@ -81,179 +120,152 @@
81 NSArray *array = [[CNGroupListAvaterUtil sharedCNGroupListAvaterUtil] requestTIMGroupInfoForChangeType:CNLiveGroupMemberChangeTypeAppFinishLaunch GroupID:nil]; 120 NSArray *array = [[CNGroupListAvaterUtil sharedCNGroupListAvaterUtil] requestTIMGroupInfoForChangeType:CNLiveGroupMemberChangeTypeAppFinishLaunch GroupID:nil];
82 [self asyncDetailGroupInfo:array]; 121 [self asyncDetailGroupInfo:array];
83 } 122 }
84 - 123 +/// 获取本地缓存的 某个群的群详情
85 - (TIMGroupInfo *)syncGetGroupInfo:(NSString *)groupId 124 - (TIMGroupInfo *)syncGetGroupInfo:(NSString *)groupId
86 { 125 {
87 return [[TIMGroupManager sharedInstance] queryGroupInfo:groupId]; 126 return [[TIMGroupManager sharedInstance] queryGroupInfo:groupId];
88 } 127 }
89 128
90 -- (void)saveGroupIdList:(BOOL)reloadLoclGrouoInfo 129 +//- (void)saveGroupIdList:(BOOL)reloadLoclGrouoInfo
  130 +//{
  131 +//
  132 +// [[TIMGroupManager sharedInstance] getGroupList:^(NSArray<TIMGroupInfoResult *> *allGroupArr) {
  133 +//
  134 +// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  135 +//
  136 +// [[CNGroupIDFMDBManager manager] deleteAllGroupId];
  137 +//
  138 +// /**存放所有群的id*/
  139 +// NSMutableArray *groupIdArray = [NSMutableArray array];
  140 +//
  141 +// for (int i = 0; i < allGroupArr.count; i ++) {
  142 +//
  143 +// TIMGroupInfo *info = allGroupArr[i];
  144 +// [groupIdArray addObject:info.group];
  145 +// /**把群id保存到数据库*/
  146 +// [[CNGroupIDFMDBManager manager] insertGroupId:info.group];
  147 +//
  148 +// /**缓存群列表*/
  149 +// IMAGroup *group = [[IMAPlatform sharedInstance].contactMgr getUserByGroupId:info.group];
  150 +// if (group == nil) {
  151 +// [[IMAPlatform sharedInstance].contactMgr onAddGroup:group];
  152 +// }
  153 +// group.groupInfo.selfInfo = info.selfInfo;
  154 +// group.groupInfo.allShutup = info.allShutup;
  155 +//
  156 +// /**把群免打扰设置保存到数据库*/
  157 +// [self saveGroupReceiveMsgStatusWithGroupInfo:info];
  158 +// }
  159 +//
  160 +// /**缓存群id到内存*/
  161 +// self.groupIdsList = groupIdArray.mutableCopy;
  162 +//
  163 +// /**是否需要刷新会话列表*/
  164 +// if (reloadLoclGrouoInfo) {
  165 +// dispatch_async(dispatch_get_main_queue(), ^{
  166 +// [[IMAPlatform sharedInstance].conversationMgr asyncConversationList];
  167 +// });
  168 +// }
  169 +//
  170 +// [self syncGroupInfoWithGroupIdArray:groupIdArray];
  171 +// });
  172 +//
  173 +//
  174 +// } fail:^(int code, NSString *msg) {
  175 +//
  176 +// }];
  177 +//}
  178 +
  179 +/// 同步群聊信息到本地
  180 +/// @param groupIdArray 要同步的所有群id
  181 +- (void)syncGroupInfoWithGroupIdArray:(NSArray *)groupIdArray
91 { 182 {
92 -  
93 - [[TIMGroupManager sharedInstance] getGroupList:^(NSArray<TIMGroupInfoResult *> *allGroupArr) { 183 + //调 getGroupInfo 方法可以让 IM把 群组信息 存到本地,然后通过 queryGroupInfo 获取本地群组信息. 每次最多只请求50个群的群详情数据,请求太多会失败
  184 +
  185 + ///通过线程组来监听所有的同步群信息 操作已完成
  186 + dispatch_group_t gcd_group = dispatch_group_create();
  187 +
  188 + if (groupIdArray.count <= 50) {
  189 + dispatch_group_enter(gcd_group);
  190 + [[TIMGroupManager sharedInstance] getGroupInfo:groupIdArray succ:^(NSArray<TIMGroupInfoResult *> *arr) {
  191 + dispatch_group_leave(gcd_group);
  192 + } fail:^(int code, NSString *msg) {
  193 + dispatch_group_leave(gcd_group);
  194 + }];
  195 + }
  196 + else
  197 + {
  198 + //分段请求,每段请求 50 个
  199 + NSInteger count = groupIdArray.count/50; //50的倍数
  200 + NSInteger remainder = groupIdArray.count%50; //50的 余数
  201 + for (int i = 0; i < count; i++) {
  202 + //每50 个请求一次
  203 + NSArray *subArray = [groupIdArray subarrayWithRange:NSMakeRange(i*50, 50)];
  204 + dispatch_group_enter(gcd_group);
  205 + [[TIMGroupManager sharedInstance] getGroupInfo:subArray succ:^(NSArray<TIMGroupInfoResult *> *arr) {
  206 + dispatch_group_leave(gcd_group);
  207 + } fail:^(int code, NSString *msg) {
  208 + dispatch_group_leave(gcd_group);
  209 + }];;
  210 + }
94 211
95 - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
96 - [[CNGroupIDFMDBManager manager] deleteAllGroupId];  
97 - NSMutableArray *groupIdArray = [NSMutableArray array];  
98 - for (int i = 0; i < allGroupArr.count; i ++) {  
99 - TIMGroupInfo *info = allGroupArr[i];  
100 - [groupIdArray addObject:info.group];  
101 - [[CNGroupIDFMDBManager manager] insertGroupId:info.group];  
102 - }  
103 - self.groupIdsList = groupIdArray;  
104 - NSArray *copyGroupIdArray = groupIdArray.copy; 212 + // 50 的余数请求
  213 + if (remainder > 0) {
105 214
106 - if (reloadLoclGrouoInfo) {  
107 - dispatch_async(dispatch_get_main_queue(), ^{  
108 - [[IMAPlatform sharedInstance].conversationMgr asyncConversationList];  
109 - });  
110 - }  
111 - //当前会话列表id  
112 - NSMutableArray *currentConversationListID = [[NSMutableArray alloc] init];  
113 - for (TIMConversation *conv in [[TIMManager sharedInstance] getConversationList]) {  
114 - if ([conv getReceiver]) {  
115 - [currentConversationListID addObject:[conv getReceiver]];  
116 - }  
117 - }  
118 - //调 getGroupInfo 方法可以让 IM把 群组信息 存到本地,然后通过 queryGroupInfo 获取本地群组信息  
119 - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
120 -  
121 - if (copyGroupIdArray.count > 50) {  
122 - //分段请求,每段请求 50 个  
123 - NSInteger count = copyGroupIdArray.count/50; //50的倍数  
124 - NSInteger remainder = copyGroupIdArray.count%50; //50的 余数  
125 - for (int i = 0; i < count; i++) {  
126 - //每50 个请求一次  
127 - NSArray *subArray = [copyGroupIdArray subarrayWithRange:NSMakeRange(i*50, 50)];  
128 - [[TIMGroupManager sharedInstance] getGroupInfo:subArray succ:^(NSArray<TIMGroupInfoResult *> *arr) {  
129 - NSLog(@"111");  
130 - for ( int j = 0; j < arr.count; j++) {  
131 - if ((i*50 + j) < allGroupArr.count) {  
132 - TIMGroupInfo *info = [allGroupArr objectAtIndex:(i*50 + j)];  
133 -// TIMGroupInfoResult *groupInfoResult = arr[j];  
134 - IMAGroup *group = [[IMAPlatform sharedInstance].contactMgr getUserByGroupId:info.group];  
135 -// [group changeGroupInfo:groupInfoResult];  
136 - group.groupInfo.selfInfo = info.selfInfo;  
137 - group.groupInfo.allShutup = info.allShutup;  
138 - if (group.groupId) {  
139 - if ([currentConversationListID containsObject:group.groupId]) {  
140 - [self saveGroupReceiveMsgStatus:group.groupId groupInfo:info];  
141 - }  
142 - }  
143 -  
144 - //判断该群是否存在当前展示的会话列表中  
145 - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
146 -  
147 - });  
148 -  
149 -  
150 - }  
151 - }  
152 - // if (i == (count-1)) {  
153 - // [self asyncConfigGroup];  
154 - // }  
155 - [self asyncConfigGroup];  
156 - } fail:^(int code, NSString *msg) {  
157 -  
158 - }];;  
159 - }  
160 -  
161 - // 50 的余数请求  
162 - NSArray *subArray = [copyGroupIdArray subarrayWithRange:NSMakeRange(count*50, remainder)];  
163 - [[TIMGroupManager sharedInstance] getGroupInfo:subArray succ:^(NSArray<TIMGroupInfoResult *> *arr) {  
164 -  
165 - for ( int j = 0; j < arr.count; j++) {  
166 - if ((count*50 + j) < allGroupArr.count) {  
167 - TIMGroupInfo *info = [allGroupArr objectAtIndex:(count*50 + j)];  
168 -// TIMGroupInfoResult *groupInfoResult = arr[j];  
169 - IMAGroup *group = [[IMAPlatform sharedInstance].contactMgr getUserByGroupId:info.group];  
170 -// [group changeGroupInfo:groupInfoResult];  
171 - group.groupInfo.selfInfo = info.selfInfo;  
172 - group.groupInfo.allShutup = info.allShutup;  
173 - if (group.groupId) {  
174 - if ([currentConversationListID containsObject:group.groupId]) {  
175 - [self saveGroupReceiveMsgStatus:group.groupId groupInfo:info];  
176 - }  
177 - }  
178 - //判断该群是否存在当前展示的会话列表中  
179 - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
180 -  
181 - });  
182 - }  
183 - }  
184 - [self asyncConfigGroup];  
185 - } fail:^(int code, NSString *msg) {  
186 -  
187 - }];  
188 -  
189 - }  
190 - else  
191 - {  
192 - [[TIMGroupManager sharedInstance] getGroupInfo:copyGroupIdArray succ:^(NSArray<TIMGroupInfoResult *> *arr) {  
193 - NSLog(@"22222");  
194 - for (int i = 0; i < allGroupArr.count; i ++) {  
195 - TIMGroupInfo *info = allGroupArr[i];  
196 -// TIMGroupInfoResult *groupInfoResult = arr[i];  
197 - IMAGroup *group = [[IMAPlatform sharedInstance].contactMgr getUserByGroupId:info.group];  
198 -// [group changeGroupInfo:groupInfoResult];  
199 - group.groupInfo.selfInfo = info.selfInfo;  
200 - group.groupInfo.allShutup = info.allShutup;  
201 - //判断该群是否存在当前展示的会话列表中  
202 - if (group.groupId) {  
203 - if ([currentConversationListID containsObject:group.groupId]) {  
204 - [self saveGroupReceiveMsgStatus:group.groupId groupInfo:info];  
205 - }  
206 - }  
207 - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
208 -  
209 - });  
210 - }  
211 - [self asyncConfigGroup];  
212 - } fail:^(int code, NSString *msg) {  
213 - NSLog(@"1111");  
214 - }];  
215 - }  
216 -  
217 -  
218 - });  
219 - });  
220 -  
221 -  
222 - } fail:^(int code, NSString *msg) { 215 + NSArray *subArray = [groupIdArray subarrayWithRange:NSMakeRange(count*50, remainder)];
  216 + dispatch_group_enter(gcd_group);
  217 +
  218 + [[TIMGroupManager sharedInstance] getGroupInfo:subArray succ:^(NSArray<TIMGroupInfoResult *> *arr) {
  219 + dispatch_group_leave(gcd_group);
  220 + } fail:^(int code, NSString *msg) {
  221 + dispatch_group_leave(gcd_group);
  222 + }];
  223 +
  224 + }
223 225
224 - }]; 226 + }
  227 +
  228 + //群聊信息都同步完成
  229 + dispatch_group_notify(gcd_group, dispatch_get_main_queue(), ^{
  230 + [self asyncConfigGroup];
  231 + });
  232 +
  233 +
  234 +
225 } 235 }
226 -//存储当前聊天会话群的消息免打扰状态  
227 -- (void)saveGroupReceiveMsgStatus:(NSString *)groupId groupInfo:(TIMGroupInfo *)info 236 +/// 存储群聊的消息免打扰状态
  237 +- (void)saveGroupReceiveMsgStatusWithGroupInfo:(TIMGroupInfo *)groupInfo
228 { 238 {
229 - //存储扩展的群资料字段  
230 - CNGroupInfoExtendModel *groupInfoExtendModel = [[CNGroupInfoExtendFMDBManager manager] searchByGroupId:groupId];  
231 - if (groupInfoExtendModel) {  
232 - //存在更新  
233 - if (info.selfInfo.recvOpt == TIM_GROUP_RECEIVE_NOT_NOTIFY_MESSAGE) {  
234 - if (groupInfoExtendModel.receiveMsg) {  
235 - if (![groupInfoExtendModel.receiveMsg isEqualToString:@"1"]) {  
236 - groupInfoExtendModel.receiveMsg = @"1";  
237 - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 239 +
  240 + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  241 +
  242 + //存储扩展的群资料字段
  243 + CNGroupInfoExtendModel *groupInfoExtendModel = [[CNGroupInfoExtendFMDBManager manager] searchByGroupId:groupInfo.group];
  244 +
  245 + if (groupInfoExtendModel) {
  246 + //存在更新
  247 + if (groupInfo.selfInfo.recvOpt == TIM_GROUP_RECEIVE_NOT_NOTIFY_MESSAGE) {
  248 + if (groupInfoExtendModel.receiveMsg) {
  249 + if (![groupInfoExtendModel.receiveMsg isEqualToString:@"1"]) {
  250 + groupInfoExtendModel.receiveMsg = @"1";
238 [[CNGroupInfoExtendFMDBManager manager] updateData:groupInfoExtendModel succ:nil fail:nil]; 251 [[CNGroupInfoExtendFMDBManager manager] updateData:groupInfoExtendModel succ:nil fail:nil];
239 - }); 252 + }
240 } 253 }
  254 +
241 } 255 }
242 -  
243 - }  
244 - }  
245 - else  
246 - {  
247 - //不存在插入  
248 - CNGroupInfoExtendModel *groupInfoExtendModel = [[CNGroupInfoExtendModel alloc] init];  
249 - groupInfoExtendModel.groupId = groupId;  
250 - if (info.selfInfo.recvOpt == TIM_GROUP_RECEIVE_NOT_NOTIFY_MESSAGE) {  
251 - groupInfoExtendModel.receiveMsg = @"1";  
252 } 256 }
253 - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 257 + else
  258 + {
  259 + //不存在插入
  260 + CNGroupInfoExtendModel *groupInfoExtendModel = [[CNGroupInfoExtendModel alloc] init];
  261 + groupInfoExtendModel.groupId = groupInfo.group;
  262 + if (groupInfo.selfInfo.recvOpt == TIM_GROUP_RECEIVE_NOT_NOTIFY_MESSAGE) {
  263 + groupInfoExtendModel.receiveMsg = @"1";
  264 + }
254 [[CNGroupInfoExtendFMDBManager manager] insterData:groupInfoExtendModel succ:nil fail:nil]; 265 [[CNGroupInfoExtendFMDBManager manager] insterData:groupInfoExtendModel succ:nil fail:nil];
255 - });  
256 - } 266 + }
  267 + });
  268 +
257 } 269 }
258 // 获取公开群 270 // 获取公开群
259 - (NSMutableDictionary *)publicGroups 271 - (NSMutableDictionary *)publicGroups
CNLiveNewIMAManagerKit/Classes/Playform/IMAContactManager.m
@@ -642,6 +642,10 @@ @@ -642,6 +642,10 @@
642 { 642 {
643 if (group) 643 if (group)
644 { 644 {
  645 + if(_groupList == nil)
  646 + {
  647 + _groupList = [[CLSafeMutableArray alloc] init];
  648 + }
645 [_groupList addObject:group]; 649 [_groupList addObject:group];
646 } 650 }
647 } 651 }