TwilioDevEd / twiliochat-swift

Swift implementation of Twilio Chat
https://www.twilio.com/docs/tutorials/walkthrough/ip-chat/ios/swift
MIT License
46 stars 18 forks source link

Cannot cast between TCHChannelDescriptor and TCHChannel as code does #32

Open kornha opened 5 years ago

kornha commented 5 years ago

Hello,

I have followed the code closely and noticed what appears to me to be a problem, but I could be wrong and would love to be shown why. Specifically, in ChannelManager.swift

        channelsList?.userChannelDescriptors { result, paginator in
            self.channels?.addObjects(from: paginator!.items())
            self.sortChannels()
        }

Adds a list of TCHChannelDescriptor to self.channels. However, if on this same list in MenuViewController.swift the following casts get called:

let channel = ChannelManager.sharedManager.channels![indexPath.row] as! AnyObject //works
let channel = ChannelManager.sharedManager.channels![indexPath.row] as! TCHChannelDescriptor //works
if let channel = ChannelManager.sharedManager.channels?.object(at: indexPath.row) as? TCHChannel // does NOT work unless a TCHChannel was independently added to the list!!

Furthermore, in ChannelManager.swift the following is called:

    func chatClient(_ client: TwilioChatClient, channelDeleted channel: TCHChannel) {
        DispatchQueue.main.async {
            if self.channels != nil {
                self.channels?.remove(channel)
            }
            self.delegate?.chatClient(client, channelDeleted: channel)
        }

    }

This will also NOT work if the channel in channels is of type TCHChannelDescriptor (which it will be if it is added from populateChannels())

I certainly hope I am missing something and this is not an enormous code oversight, so please let me know what I missed.

Thank you tons.

jefflinwood commented 5 years ago

The code is unclear, and incorrect - I think what would make this much more approachable would be to rename the ChannelManager.sharedManager.channels as ChannelManager.sharedManager.channelDescriptors, to make the distinction between the TCHChannel and the TCHChannelDescriptor much more clear in the code.

kornha commented 5 years ago

Thank you for your reply.

I can make the distinction between the TCHChannel and TCHDescriptor set. However, in my (and my guess, most any) 'MenuTableView' cells, we need to fetch the most recent message to display on the cell. Furthermore, when deleting a channel from the Table, we will delete the TCHChannel, not the descriptor. Which means that, in a general use case we must fetch the TCHChannel. However, we cannot fetch the channels directly from the channelsList. Therefore, each time we fetch channels we must make 2 hops, which is unnecessary latency and slows down our app.

This is my assumption and I would love to be corrected so I can correct my approach.

devlove1C commented 5 years ago

Hello @kwhinnery I am facing a issue In chat implementation. My code is working properly but when i am sending message or receiving message the delegate method

func chatClient(_ client: TwilioChatClient, channel: TCHChannel, messageAdded message: TCHMessage) { 
}

called two times (Except this all delegate method called once) Can you let me know the reason for this Why its happening Please Let me know if you have any idea about this issue

Thanks

jefflinwood commented 5 years ago

@devlove1C - can you open another issue for this, and include some more code from your application that shows how you set the delegate?