Skip to main content

Get DM Chatrooms

Retrieves DM chatrooms and provides real-time updates using a Realm observer. This function allows users to fetch chatrooms and stay updated on any changes to the chatroom list.

Steps to get chatrooms with observer​

  1. Create an object that conforms to the HomeFeedClientObserver protocol.
  2. Implement the required methods of the protocol in your object.
  3. Call getDMChatrooms(withObserver:) function using the instance of LMChatClient.
  4. Process the initial data and subsequent changes in your observer methods.
class MyChatroomObserver: HomeFeedClientObserver {
func initial(_ chatrooms: [Chatroom]) {
// Process initial list of chatrooms
}

func onChange(removed: [Chatroom], inserted: [(Int, Chatroom)], updated: [(Int, Chatroom)]) {
// Handle changes to the chatroom list
}
}

let observer = MyChatroomObserver()
LMChatClient.shared.getChatrooms(withObserver: observer)
note

This function uses a Realm observer to provide real-time updates. Make sure to keep a strong reference to your observer object to prevent it from being deallocated.

Protocols​

HomeFeedClientObserver​

  • initial(_ chatrooms: [Chatroom]): This method is called when the initial list of chatrooms is fetched. Use this to set up your initial UI or data structures.

  • onChange(removed: [Chatroom], inserted: [(Int, Chatroom)], updated: [(Int, Chatroom)]): This method is called whenever there are changes to the chatroom list:

    • removed: An array of chatrooms that have been removed.
    • inserted: An array of tuples containing the index and the new chatroom that has been inserted.
    • updated: An array of tuples containing the index and the chatroom that has been updated.