# Observing channels just after user is signed in doesn't work

**URL:** <https://forum.social.plus/t/observing-channels-just-after-user-is-signed-in-doesnt-work/1285>\
**Category:** iOS\
**Created:** [April 26, 2024, 9:13am UTC](https://forum.social.plus/t/observing-channels-just-after-user-is-signed-in-doesnt-work/1285 "2024-04-26T09:13:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![nadol](https://avatars.discourse-cdn.com/v4/letter/n/3da27b/32.png) [@nadol](https://forum.social.plus/u/nadol)\
**Post date:** [April 26, 2024, 9:13am UTC](https://forum.social.plus/t/observing-channels-just-after-user-is-signed-in-doesnt-work/1285/1 "2024-04-26T09:13:10Z")

</div>

I’m trying to implement channels observation after user is signed in and I can’t make it to work, the observe closure is not called. It works fine when I kill the app and when the app is relaunched, but it doesn’t right after when user is logged in. I’m getting these warnings printed out in the console:

```auto
🟨 [AmitySDK]: Cannot fetch network configuration for chat module. User is not logged in. Use login() before calling any api.
🟨 [AmitySDK]: AmitySDK cannot subscribe to user deletion event (unable to find the current user).
🟨 [AmitySDK]: AmitySDK cannot subscribe to user deletion event (unable to find the current user).
🟨 [AmitySDK]: AmitySDK cannot subscribe to user deletion event (unable to find the current user).

```

these warning are triggered when I call the `getChannels(channelIds:)` function on object of `AmityChannelRepository`

I’m using AmityUIKit 3.20.3

Here’s what I’m doing:

1. I’m initialising `AmityClient` and `AmityUIKitManager` using my apiKey and region
2. I’m signing in user, fetching auth token from my server and then calling `AmityUIKitManager.registerDevice(withUserId:displayName:authToken:sessionHandler:completion)` function and waiting for completion block to be invoked and making sure that the parameter of `Bool` type is true
3. I’m observing `.amityClient.$sessionState` and when it’s `.established` I’m creating object of `AmityChannelRepository` type and calling:

```auto
        let notificationToken = channelRepository
            .getChannels(channelIds: ids)
            .observe { liveCollection, _, error in

```

but the observe block is never called and I’m getting the warnings mentioned above printed in the console.

So I’m making sure that the `getChannels` is called only when user is logged in and session state is established but it doesn’t work. It works when I kill the app and launch it when the user is already logged in though.

I tried to investigate what are the differences in `AmitySDK.AmityCollection<AmitySDK.AmityChannel>` object returned from `getChannels` function when I sign in the user vs. when the app is launched with the user already logged in, and I found that in the first case `result.transaction.currentUserId` is nil while in the second case the `currentUserId` has the value set.

Any ideas what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [April 26, 2024, 10:32am UTC](https://forum.social.plus/t/observing-channels-just-after-user-is-signed-in-doesnt-work/1285/2 "2024-04-26T10:32:03Z")

</div>

@nadol To ensure the Amity notification token works correctly in your implementation, it’s important to declare it at the class level. This helps prevent the token from being dismissed prematurely.

For detailed instructions on declaring and managing the Amity notification token, please see our documentation: [Amity SDK Documentation](https://docs.amity.co/amity-sdk/core-concepts/live-objects-collections/ios#amitynotificationtoken).

---

<div class="post-metadata">

**Author:** ![nadol](https://avatars.discourse-cdn.com/v4/letter/n/3da27b/32.png) [@nadol](https://forum.social.plus/u/nadol)\
**Post date:** [April 26, 2024, 10:46am UTC](https://forum.social.plus/t/observing-channels-just-after-user-is-signed-in-doesnt-work/1285/3 "2024-04-26T10:46:44Z")

</div>

@SocialPlus_Support I have it declared at the class level. I simplified my code so you can take a look:

```auto
final class ChatChannelsRepository {
    struct Dependencies {
        let amityClient: AmityClient
        let eventsStorage: EventsStorage
    }

    private let dependencies: Dependencies
    private var cancellables = [AnyCancellable]()
    private var channelRepository: AmityChannelRepository?
    private var token: AmityNotificationToken?

    init(dependencies: Dependencies) {
        self.dependencies = dependencies
        setupBindings()
    }

    private func setupBindings() {
        dependencies.amityClient.$sessionState
            .sink(receiveValue: { [weak self] in
                switch $0 {
                case .established:
                    if let self, channelRepository == nil {
                        self.channelRepository = .init(client: self.dependencies.amityClient)
                    }
                    self?.observeChannels(ids: [
                        "bei.lgdBoSXVJMYcedg73uvqOK7cBdW2.1714067336810",
                        "bei.lgdBoSXVJMYcedg73uvqOK7cBdW2.1714068108073"
                    ])
                default:
                    break
                }
            })
            .store(in: &cancellables)
    }

    private func observeChannels(ids: [String]) {
        guard let channelRepository else {
            return
        }

        token = channelRepository
            .getChannels(channelIds: ids)
            .observe { liveCollection, _, error in
                if let error = error {
                    return
                }

                let chatChannels = liveCollection.snapshots.map {
                    ChatChannel(
                        id: $0.channelId,
                        name: $0.displayName ?? "General",
                        unreadCount: $0.subChannelsUnreadCount
                    )
                }
                print("chatChannelsCount \(chatChannels.count)")
            }
    }
}

```

---

<div class="post-metadata">

**Author:** ![nadol](https://avatars.discourse-cdn.com/v4/letter/n/3da27b/32.png) [@nadol](https://forum.social.plus/u/nadol)\
**Post date:** [April 26, 2024, 10:50am UTC](https://forum.social.plus/t/observing-channels-just-after-user-is-signed-in-doesnt-work/1285/4 "2024-04-26T10:50:03Z")

</div>

If you look at the warnings in my original post then it seems like I’m calling observe before user is logged in but the problem is that I call it after making sure that user got logged in and session state is established

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [April 26, 2024, 11:13am UTC](https://forum.social.plus/t/observing-channels-just-after-user-is-signed-in-doesnt-work/1285/5 "2024-04-26T11:13:55Z")

</div>

@nadol Let me pass this on to my team, and I’ll get back to you.

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [April 29, 2024, 9:35am UTC](https://forum.social.plus/t/observing-channels-just-after-user-is-signed-in-doesnt-work/1285/6 "2024-04-29T09:35:28Z")

</div>

@nadol After reviewing your code, could you please verify whether this code successfully completes the authentication process before proceeding to `setupBindings()`?

```swift
init(dependencies: Dependencies) {
    self.dependencies = dependencies
    setupBindings()
}

```

This code does not seem to confirm that the authentication process has been successful. Since the value of `channelRepository` does not necessarily indicate that the session has already been established, consider this modification:

```swift
if channelRepository == nil {
    self.channelRepository = .init(client: self.dependencies.amityClient)
}

```

This adjustment ensures that `channelRepository` is only initialized once the session has been established, providing a more reliable setup.

---

<div class="post-metadata">

**Author:** ![nadol](https://avatars.discourse-cdn.com/v4/letter/n/3da27b/32.png) [@nadol](https://forum.social.plus/u/nadol)\
**Post date:** [April 29, 2024, 10:17am UTC](https://forum.social.plus/t/observing-channels-just-after-user-is-signed-in-doesnt-work/1285/7 "2024-04-29T10:17:50Z")

</div>

I don’t understand what has been changed?

```auto
                    if let self, channelRepository == nil {
                        self.channelRepository = .init(client: self.dependencies.amityClient)
                    }
                    self?.observeChannels(ids: [
                        "bei.lgdBoSXVJMYcedg73uvqOK7cBdW2.1714067336810",
                        "bei.lgdBoSXVJMYcedg73uvqOK7cBdW2.1714068108073"
                    ])

```

this code from my original snippet is called only when session is established

---

<div class="post-metadata">

**Author:** ![nadol](https://avatars.discourse-cdn.com/v4/letter/n/3da27b/32.png) [@nadol](https://forum.social.plus/u/nadol)\
**Post date:** [April 29, 2024, 10:20am UTC](https://forum.social.plus/t/observing-channels-just-after-user-is-signed-in-doesnt-work/1285/8 "2024-04-29T10:20:15Z")

</div>

`ChatChannelsRepository` is initialised only when user successfully signs in, it is when completion block of `AmityUIKitManager.registerDevice(withUserId:displayName:authToken:sessionHandler:completion)` is called

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [April 30, 2024, 11:22am UTC](https://forum.social.plus/t/observing-channels-just-after-user-is-signed-in-doesnt-work/1285/9 "2024-04-30T11:22:37Z")

</div>

Thank you @nadol for additional information. We passed this back to the team.

---

<div class="post-metadata">

**Author:** ![nadol](https://avatars.discourse-cdn.com/v4/letter/n/3da27b/32.png) [@nadol](https://forum.social.plus/u/nadol)\
**Post date:** [May 1, 2024, 11:18am UTC](https://forum.social.plus/t/observing-channels-just-after-user-is-signed-in-doesnt-work/1285/10 "2024-05-01T11:18:20Z")

</div>

@SocialPlus_Support I think I found a workaround for this issue. It seems like amity client can’t properly update the status of session if it’s created before `AmityUIKitManager.registerDevice(withUserId:displayName:authToken:sessionHandler:completion)` is executed and completed and that was causing the warnings. If I initialise `AmityClient` only when session is established then it works fine

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [May 2, 2024, 1:03pm UTC](https://forum.social.plus/t/observing-channels-just-after-user-is-signed-in-doesnt-work/1285/11 "2024-05-02T13:03:09Z")

</div>

@nadol After further investigation, it was found to be the expected behavior from the SDK, and your report to us was correct. We have informed the relevant team to correct our documentation accordingly. Thank you for sharing your findings.
