1. 程式人生 > >iOS直播Liveroom組件,遊客,用戶多次切換登錄同一直播間,消息出現多次重復問題解決

iOS直播Liveroom組件,遊客,用戶多次切換登錄同一直播間,消息出現多次重復問題解決

with handle roo 遇到 format 重復 con 單例 serve

byzqk

新版,加入連麥功能,直播的流程修改很多,每次登錄都需要登錄liveroom組件

期間遇到一個奇葩的問題,就是遊客登錄組件之後,切換為用戶登錄,出現im消息重復的問題,一開始以為是遊客退出不成功,導致的問題,然後一直找這個問題,確定遊客百分之百退出了。。。以為該好了,可是。。。。效果,依然是那種情況,並無卵用,然後,開始了痛苦的一步一步打斷點流程。。

組件Liveroom是單例,每次登錄都會初始化這個對象的內容

主要問題就在這個方法

- (void)login:(NSString*)serverDomain loginInfo:(LoginInfo *)loginInfo withCompletion:(ILoginCompletionHandler)completion {
    [self asyncRun:^{
        // 保存到本地
        _serverDomain = serverDomain;
        
        [self login:loginInfo.sdkAppID accType:loginInfo.accType userID:loginInfo.userID userSig:loginInfo.userSig completion:^(int errCode, NSString *errMsg, NSString *userID, NSString *token) {
            if (errCode == ROOM_SUCCESS) {
                
                completion(0, @"RoomService登錄成功");
                
                [self initApiAddr: loginInfo.userID token:token];
                
                _appID = [NSString stringWithFormat:@"%d", loginInfo.sdkAppID];
                
                // 初始化userInfo
                _userInfo = [SelfAccountInfo new];
                _userInfo.userID    = loginInfo.userID;
                _userInfo.userName  = loginInfo.userName;
                _userInfo.userAvatar = loginInfo.userAvatar;
                _userInfo.sdkAppID = loginInfo.sdkAppID;
                _userInfo.accType = loginInfo.accType;
                _userInfo.userSig = loginInfo.userSig;
                
              
                // 初始化 RoomMsgMgr 並登錄
                RoomMsgMgrConfig *config = [[RoomMsgMgrConfig alloc] init];
                config.userID = loginInfo.userID;
                config.appID = loginInfo.sdkAppID;
                config.accType = loginInfo.accType;
                config.userSig = loginInfo.userSig;
                config.userName = loginInfo.userName;
                config.userAvatar = loginInfo.userAvatar;
                config.level = loginInfo.level;
                
                //消息重復---註意多次登錄組件,就會多次初始化下邊這個對象,他是監測消息的,登錄幾次組件,就會有幾個這樣的對象,消息就會重復幾次
                //雖然遊客退出了,但是這個對象不釋放,他就會監聽消息
                if (_msgMgr) {
                    _msgMgr.delegate = nil;
                    [_msgMgr arcDebugRelease];
                }
                _msgMgr = [[RoomMsgMgr alloc] initWithConfig:config];
                [_msgMgr setDelegate:self];
                
//                [self sendDebugMsg:[NSString stringWithFormat:@"初始化IMSDK: appID[%d] userID[%@]", config.appID, config.userID]];
                
                
                //IM已經在外頭成功,這兒就不進行登錄了
                
//                __weak __typeof(self) weakSelf = self;
//                [_msgMgr login:^(int errCode, NSString *errMsg) {
//                    [weakSelf asyncRun:^{
//                        [self sendDebugMsg:[NSString stringWithFormat:@"IM登錄返回: errCode[%d] errMsg[%@]", errCode, errMsg]];
//                        if (errCode == 0 && completion) {
//                            completion(0, @"登錄成功");
//                        } else if (errCode != 0 && completion) {
//                            completion(ROOM_ERR_IM_LOGIN, @"登錄失敗");
//                        }
//                    }];
//                }];
            }
            else {
                [self sendDebugMsg:[NSString stringWithFormat:@"初始化LiveRoom失敗: errorCode[%d] errorMsg[%@]", errCode, errMsg]];
            }
        }];
    }];
}

所有的信息都會被更新,然後對象會重新初始化,但是就是這個對象,坑的一筆

  _msgMgr = [[RoomMsgMgr alloc] initWithConfig:config];
                [_msgMgr setDelegate:self];
                

需要判斷之前單例裏邊的該對象存在,需要將他釋放掉,騰訊沒有遊客登錄,所以不需要這樣的變態邏輯

 //消息重復---註意多次登錄組件,就會多次初始化下邊這個對象,他是監測消息的,登錄幾次組件,就會有幾個這樣的對象,消息就會重復幾次
                //雖然遊客退出了,但是這個對象不釋放,他就會監聽消息
                if (_msgMgr) {
                    _msgMgr.delegate = nil;
                    [_msgMgr arcDebugRelease];
                }

上邊這幾句就是解決辦法

總結一下:變態的流程,總會有變態的坑!

iOS直播Liveroom組件,遊客,用戶多次切換登錄同一直播間,消息出現多次重復問題解決