How to join multiple channels for a user in IOS swift

How to join multiple channels for a user in IOS swift.

Hello @vibhorMaheshwari , To join multiple channels for a user in iOS Swift, we suggest referring to the documentation provided at : Joining and Leaving Channels You will need to implement a loop to join multiple channels as per your requirement. This approach allows for a flexible integration, enabling users to efficiently join any number of channels.

we are trying already in a loop, there are 5 channels in an array for which loop is running, and from this array 2 channels are already joined and need to join for remain 3, loops is going for all 5 and not joined for our remain channels.

@vibhorMaheshwari Are there any errors? We need your complete curl commands to investigate these issues.

There is no error, Below is my IOS swift code:

func joinChannels(user: CustomerModel?, channelRepository: AmityChannelRepository) async throws {
        for i in (user?.amityOpenChannels ?? []).indices {
            do {
                let channel = try await channelRepository.joinChannel((user?.amityOpenChannels![i])!)
                print("Sucessfully join a channel: \(channel.object)")
                
                if (user?.amityOpenChannels?.count ?? 0) == (i + 1) {
                    Loader.hide()
                    self.gotoHomePage()
                 }
                
            } catch {
                print("Unable to join a channel: \(error)")
            }
        }
        
    }

@vibhorMaheshwari Could you please specify the types of channels you are attempting to join? This information will help us provide you with more accurate support.

we have created community type channels and we are trying to join.

Thank you. Are they private communities?

no, all they are public communities

Hello, please try the sample code below:

func joinChannels(user: CustomerModel?, channelRepository: AmityChannelRepository) async throws {
    let channels = user?.amityOpenChannels ?? []
    var joinCount = 0
    
    for channel in channels {
        do {
            let joinedChannel = try await channelRepository.joinChannel(channel)
            print("Successfully joined channel: \(joinedChannel.object)")
            joinCount += 1
            
            if joinCount == channels.count {
                Loader.hide()
                self.gotoHomePage()
            }
        } catch {
            print("Unable to join channel: \(error)")
        }
    }
}

And kindly inform us of any errors encountered during the execution.

ok, we are trying, please give us some time.

@SocialPlus_Support we have tried but not succeed, is show thee time :
Successfully joined channel: nil
Successfully joined channel: nil
Successfully joined channel: nil

@vibhorMaheshwari There is no error, Below is my IOS swift code:

func joinChannels(user: CustomerModel?, channelRepository: AmityChannelRepository) async throws {
        let channels = user?.amityOpenChannels ?? []
        var joinCount = 0
        var attemptCount = 0 // Counter for attempts
        
        for channel in channels {
            attemptCount += 1 // Increment attempt counter for each channel
            do {
                let joinedChannel = try await channelRepository.joinChannel(channel)
                print("Successfully joined channel: \(joinedChannel.object)")
                joinCount += 1
            } catch {
                print("Unable to join channel: \(error)")
            }
            
            print("attepmpt: \(attemptCount)")
        }
             
           
                print("Attempted to join \(channels.count) channels. Successfully joined \(joinCount) channels.")
    }

hi, we have tried this code also, but still not joined remain 3 channels.

output from your given code is follows

Successfully joined channel: nil
attepmpt: 1
Successfully joined channel: nil
attepmpt: 2
Successfully joined channel: nil
attepmpt: 3
Attempted to join 3 channels. Successfully joined 3 channels

Hello @vibhorMaheshwari, could you please share your complete code along with the channel IDs to our support email at support.asc@amity.co? Our team will assist you further with the investigation. Thank you.