How to Populate MessagesList UI with messages i got from PubNub fetchMessagesHistory function in Swift

Viewed 49

I am able to fetch the history messages for a channel and I need to put the array of those messages into the prebuilt UI MessagesList that they gave me, they mention in their docs that I should override or subclass but how to do that exactly? I am still a beginner with swift, I'm using UIKit not SwiftUI. thanks in advance

1 Answers

To clarify, MessageList is an inherited class of UIViewController, and is used to display a list of stored message objects. The mention of overriding core functionality is discussing using the default MessageListComponentViewModel to display the messages of a particular channel. The docs are explaining that you only need to use the MessageListComponentViewModel to obtain the history messages of a channel, but that you could also add your own overridden functionalities from the base UIViewController if wanted to do so.

For the problem at hand, after consulting with our PubNub engineers, you'll need to populate your local database with data. For example, if you want Messages to appear for a Channel, then you can use either the load(messages:) method if you have Messages already created locally, or thesyncRemoteMessages(_:) method if you want to sync messages from PubNub storage.

I strongly recommend following the Getting Started guide. The guide shows how to create local objects for use with Message List UI in a real chat application, and you can use the Message Input (at the bottom of the UI) to publish messages.

If you would like another example, you can follow PubNub's tutorial on adding iOS Chat Components to an existing application.

Related