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:
🟨 [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:
I’m initialising AmityClient and AmityUIKitManager using my apiKey and region
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
I’m observing .amityClient.$sessionState and when it’s .established I’m creating object of AmityChannelRepository type and calling:
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.
@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.
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
@nadol After reviewing your code, could you please verify whether this code successfully completes the authentication process before proceeding to 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:
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.
ChatChannelsRepository is initialised only when user successfully signs in, it is when completion block of AmityUIKitManager.registerDevice(withUserId:displayName:authToken:sessionHandler:completion) is called
@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
@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.