美文网首页初见
多个请求加载完成后,才做进一步操作,用到了dispatch_se

多个请求加载完成后,才做进一步操作,用到了dispatch_se

作者: BestRivenLG | 来源:发表于2017-08-28 18:25 被阅读77次

    不多说直接上代码

    // 已经同意并且加入群组后的回调
    - (void)didAcceptInvitationFromGroup:(EMGroup *)group
                                   error:(EMError *)error
    {
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
        __block NSString *aliasName;
        dispatch_group_t group1 = dispatch_group_create();
        dispatch_group_async(group1, dispatch_get_global_queue(0,0), ^{
            // 并行执行的线程一
            NSString *identify = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"identifySaves"]];
            NSMutableDictionary *dic = [NSMutableDictionary new];
            [dic setValue:[ToolUtils readToken]  forKey:@"token"];
            [dic setValue:[ToolUtils getPreferredLanguage] forKey:@"lang"];
            [dic setValue:identify forKey:@"id"];
    
            [AFHttpManager postJSONWithUrl: MainUrl(@"/im/getInfoById")  parameters:dic success:^(id responseObject) {
                if (responseObject) {
                    NSNumber *cid = [[responseObject objectForKey:@"opResult"] objectForKey:@"retCode"];
                    if ([cid intValue] == Success) {
                        NSArray *arr = [responseObject objectForKey:@"PersonInfo"];
                        NSDictionary* Datadic;
                        if (arr.count>0) {
                            Datadic = arr[0];
                        }
                        NSString *str = [Datadic objectForKey:@"aliasName"];
                        aliasName = str;
    
                        //请求成功,获取完昵称,  发送一个信号
                        dispatch_semaphore_signal(semaphore);
                    }
                }
            } failure:^(NSError *error) {
                aliasName = identify;
            }];
        });
        
        __block NSInteger blockType = 1;
        dispatch_group_async(group1, dispatch_get_global_queue(0,0), ^{
            // 并行执行的线程二
            NSMutableDictionary *dic = [NSMutableDictionary new];
            [dic setValue:[ToolUtils readToken]  forKey:@"token"];
            [dic setValue:group.groupId forKey:@"imname"];//[array componentsJoinedByString:@","] forKey:@"imname"];
            [dic setValue:@"2" forKey:@"type"];
            [dic setValue:[ToolUtils getPreferredLanguage] forKey:@"lang"];
            [AFHttpManager postJSONWithUrl:MainUrl(Post_imGetListInfo) parameters:dic success:^(id responseObject) {
                [[BaseViewController sharedManager] waitView].hidden = YES;
                if (responseObject) {
                    //  注意要加限制
                    NSNumber *cid = [[responseObject objectForKey:@"opResult"] objectForKey:@"retCode"];
                    if([cid intValue] == Success){
                        NSString *ID;
                        for( NSDictionary *findAllInfo in [responseObject objectForKey:@"findAllInfo"]){
                            ID = [[findAllInfo objectForKey:@"id"] stringValue];
                        }
                        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
                        [dict setValue:[ToolUtils readToken]  forKey:@"token"];
                        [dict setValue:ID forKey:@"id"];
                        [dict setValue:[ToolUtils getPreferredLanguage] forKey:@"lang"];
                        [AFHttpManager postJSONWithUrl:MainUrl(Post_imLookGroupInfo) parameters:dict success:^(id responseObject) {
                            if (responseObject) {
                                NSNumber *cid = [[responseObject objectForKey:@"opResult"] objectForKey:@"retCode"];
                                if ([cid intValue] == Success) {
                                    blockType = [[responseObject objectForKey:@"type"] integerValue];
    
                                    //获取blockType的判定条件, 发送另一个信号
                                    dispatch_semaphore_signal(semaphore);
                                }
                            }
                        } failure:^(NSError *error) {
    
                        }];
                    }
                }
            } failure:^(NSError *error) {
    
            }];
    
        });
        
        dispatch_group_notify(group1,dispatch_get_global_queue(0, 0), ^{
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);//信号量 -1
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);//信号量 -1
    
            //注意:    n个 dispatch_semaphore_signal(semaphore)  
            // 要对应   n个 dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    
            //汇总结果
            EMChatText *txtChat;
            if (blockType == 1) {//请求获取的  aliasName  和判定条件  blockType
                txtChat = [[EMChatText alloc] initWithText:[NSString stringWithFormat:@"%@加入了群",aliasName]];
            }
            
            //环信发送消息的方法
            EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithChatObject:txtChat];
            NSDictionary *loginInfo = [[[EaseMob sharedInstance] chatManager] loginInfo];
            NSString *username = [loginInfo objectForKey:kSDKUsername];
            EMMessage *message = [[EMMessage alloc] initWithReceiver:username bodies:@[body]];
            message.from = group.groupSubject;
            message.to = group.groupId;
            message.messageType = eConversationTypeGroupChat;// 设置为群聊消息
            message.deliveryState = eMessageDeliveryState_Delivered;
            message.ext = @{@"action":@"join_group"};
            [[EaseMob sharedInstance].chatManager asyncResendMessage:message progress:nil];
         });
    }
    

    相关文章

      网友评论

        本文标题:多个请求加载完成后,才做进一步操作,用到了dispatch_se

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