2

I have a table view that I am trying to make reactive. I have it working using Swift.

Following is the RxSwift code

viewModel.getDetailMessages().asObservable().bind(to: messageTableView.rx.items){ tableView, row, element in
            let indexPath = IndexPath(row: row, section: 0)
            print("The size is", InboxData.sharedInstance.inboxdata[messageIndexPath.row].messageDetail.count)
            print("The size is element", element)
            if !element.isSender{
                let cell = tableView.dequeueReusableCell(withIdentifier: "recieverCell", for: indexPath) as! RecieverMessageTableViewCell
                cell.message.text = element.messageText
                cell.message.preferredMaxLayoutWidth = 287
                cell.message.layer.cornerRadius = 8
                cell.message.layer.masksToBounds = true
                return cell
            }else{
                let cell = tableView.dequeueReusableCell(withIdentifier: "senderCell", for: indexPath) as! SenderMessageTableViewCell
                cell.senderMessage.text = element.messageText
                cell.senderMessage.layer.cornerRadius = 8
                cell.senderMessage.preferredMaxLayoutWidth = 287
                cell.senderMessage.layer.masksToBounds = true
                return cell
            }
        }

Following is my view model

class MessagwViewModel{

    func getDetailMessages() -> Observable<[Message]> {
        return Observable.just(InboxData.sharedInstance.inboxdata[messageIndexPath.row].messageDetail)
    }

}

And this is the message data class

class MessageData {

    static let sharedInstance = MessageData()

    var message1 = Message(profilePicture: "Taylor Ward", artistName: "Taylor Ward", messageText: "Hi How are you?", isSender: true)

    var messages = [Message]()

    private init() {
        loadMessages()
    }

    func loadMessages() {
        messages.removeAll()
        messages.append(message1)

    }

}

On Click of a button I am trying to append data to the Message array as follows

sendButton.rx.tap.bind{ value in
            InboxData.sharedInstance.inboxdata[messageIndexPath.row].messageDetail.append(Message(profilePicture: "", artistName: "", messageText: self.messageTextView.text, isSender: true))

       self.messageTableView.reloadData()
            self.messageTableView?.scrollToBottom()
            print("Button clicked")
        }

The count of the array increases every time I append a new element . But the table view does not reload even though the data source has the latest value

Can anyone one point out what the problem is? Thank you.

A.S
  • 798
  • 1
  • 10
  • 32

1 Answers1

2

Your problem is in the MessagwViewModel. Observable.just only streams out one element and then completes. It doesn't monitor the object for changes. This simplest fix (i.e. hack) is to make messageDetail a Variable. You don't show how that variable is defined, but based on how it is called, I assume it is currently like this: var messageDetail: [Message]. Change it to let messageDetail: Variable<[Message]>. Assign to it by using messageDetail.value = ... and read from it through its Observable messageDetail.asObservable()

Daniel T.
  • 32,821
  • 6
  • 50
  • 72