Chat client life cycle - Bandyer/Bandyer-iOS-SDK GitHub Wiki

The Kaleyra Video chat client is the component responsible for managing the connection with our chat service. If you are integrating a 2.x version of the SDK you should take a look a this guide instead.

Table of contents

Life cycle

The life cycle of the Kaleyra Video chat client is very simple and inspired by the call client life cycle. It can be in several states, you can inspect in which one the client is at any moment in time querying its state property. Beware, you must configure the SDK adding the Chat tool during configuration, otherwise the chat client won't be started by the SDK. It begins its lifecycle in the stopped state, and while in this state chat services won't be available. Once started, the client will eventually transition to the running state. While in that state all chat services will be available to your user and she / he will be able to send and receive messages. When your app goes in background, the chat client is paused automatically, closing any connection to Bandyer platform, and it resumes automatically when your app returns in foreground. As you might expect, while in those states the chat client cannot perform any task, nor it can receive or send any message. The SDK will automatically resume the connection to the chat service when your app returns in foreground.

State changes

In order to be notified about chat client state changes, you should subscribe as a chat client observer conforming to the ChatClientObserver protocol. Here's a snippet of code showing you how to do it:

class MyViewController: UIViewController, ChatClientObserver {

    func subscribeAsChatClientObserver() {
        BandyerSDK.instance.chatClient.add(observer: self, queue: .main)
    }
}
@interface MyViewController <BDKChatClientObserver>
@end

@implementation MyViewController

- (void)subscribeAsChatClientObserver
{
    [BandyerSDK.instance.chatClient addObserver:self queue:dispatch_get_main_queue()];
}

@end

The chat client will keep a weak reference to its observers, so if you keep a strong reference to the client you can safely subscribe as an observer and you won't get a retain cycle. The chat client has two method overloads for adding an object as an observer, the first method is addObserver: (add(observer:)) in swift) the second one is addObserver:queue: (add(observer:queue:)) in swift) (the former is just a convenience method that will call the latter providing a nil queue reference), as stated in our code documentation if you provide a nil queue as the second argument of the addObserver:queue:, your observer will be called synchronously on a background private queue, whereas if you provide a queue as the second argument in the method call, the observer will be called asynchronously on the queue you provided. You must take that into account if you manipulate your UI from one of the observer protocol method.

What's next