GetStream / stream-chat-swift

💬 iOS Chat SDK in Swift - Build your own app chat experience for iOS using the official Stream Chat API
https://getstream.io/chat/sdk/ios/
Other
849 stars 204 forks source link

Messages in channel fail to load/appear #2229

Closed AndrewSB closed 1 year ago

AndrewSB commented 2 years ago

What did you do?

tried to scroll to the top of a channel, to see my older messages

What did you expect to happen?

being able to scroll all the way to the beginning of a thread

What happened instead?

instead, parts of my history were unavailable, i couldn't scroll back past a point

I see the following logs as i scroll to the top of the channel :

2022-08-10 09:27:19.362 [ERROR] [com.apple.NSURLSession-delegate] [RequestDecoder.swift:51] [decodeRequestResponse(data:response:error:)] > API request failed with status code: 400, response:
{
  "code" : 4,
  "message" : "GetOrCreateChannel failed with error: \"Message with id 96c68774-1ad1-418b-a45a-c8974d21aafc doesn't exist\"",
  "more_info" : "https:\/\/getstream.io\/chat\/docs\/api_errors_response",
  "StatusCode" : 400,
  "duration" : "0.00ms"
})

the oldest message being displayed in the channel is a message that failed to send, my guess is the SDK encounters this message that's failed to send, requests it from the server, receives an error and then stops fetching older messages for some reason.

not sure if this should be here or in the low level SDK

martinmitrevski commented 2 years ago

Hey @AndrewSB,

Thanks for reporting this. Moving it to our low-level client repo, since the issue is there. We will analyze it and get back to you when we have an update.

In the meantime, deleting the message or sending new ones to the channel should solve it for that particular channel.

Best, Martin

polqf commented 2 years ago

Hi @AndrewSB , we believe to have found the issue, which is fixed here: https://github.com/GetStream/stream-chat-swift/pull/2258 Could you give it a try and let us know if it works?

nuno-vieira commented 2 years ago

Hi!

This issue is now solved in the 4.21.1 release.

Thank you for your patience!

Best, Nuno

AndrewSB commented 2 years ago

Hey! we just tested with the new version and it looks like the problem is actually worse, now we see this more often even when .isLocalStorage = false

I can get some logs for you sometime tomorrow, maybe the underlying issue wasn't https://github.com/GetStream/stream-chat-swift/pull/2258

nuno-vieira commented 2 years ago

Hi @AndrewSB,

Just clarify, for this to happen, the 25 message needs to fail right?

Yes, logs would be nice very helpful.

We will re-open the issue then and resume the investigation, sorry for the inconvenience.

Best, Nuno

AndrewSB commented 2 years ago

so it looks like the logs i see both when i open the channel, and when i scroll to the top are

2022-09-08 13:37:57.892 [ERROR] [main] [ChannelController.swift:741] [loadPreviousMessages(before:limit:completion:)] > You can't load new messages when there is no messages in the channel.

repeated many times

let me see if i can get more details.

AndrewSB commented 2 years ago

i think i understand the bug, it seems like incorrect state in the ChannelController somehow.

on ChannelController.swift:741, both messageId and lastFetchedMessageId are nil, so it's never able to load any previous messages

this is on release 4.21.1. let me try the latest to see if it's any different

AndrewSB commented 2 years ago

there must be an edge case that was missed in https://github.com/GetStream/stream-chat-swift/pull/2258

let me know how i can help to solve this

polqf commented 2 years ago

Hi @AndrewSB. Could you please let us know what's the flow that leads to calling loadPreviousMessages for you? You should have called synchronize before, that should have set a value for lastFetchedMessageId. We're investigating it.

polqf commented 2 years ago

We have created a branch for you to test: https://github.com/GetStream/stream-chat-swift/pull/2279

But we believe there can be something you are doing within your flow that's not following the expected order (synchronize -> loadPreviousMessages). It is looks like you were only fetching from DB. Please give us some more context so we are sure we are solving the issue for you

AndrewSB commented 2 years ago

@martinmitrevski i think this might be related to that workaround we did to prevent being booted out of a chat thread

this is how we create a channel destination, i don't believe its synchronized in the solution we came up with together

AndrewSB commented 2 years ago

going to try doing a chatController.synchronize() right before we return the ChatThreadView

AndrewSB commented 2 years ago

ok, still doesn't work, but now what i see is a bunch of

2022-09-09 19:19:58.709 [ERROR] [com.apple.NSURLSession-delegate] [RequestDecoder.swift:67] [decodeRequestResponse(data:response:error:)] > API request failed with status code: 429, code: 9 response:
{
  "code" : 9,
  "message" : "GetOrCreateChannel failed with error: \"Too many requests for user: 5cb05d045b81370e679a1fcb, check response headers for more information.\"",
  "more_info" : "https:\/\/getstream.io\/chat\/docs\/api_errors_response",
  "details" : [

  ],
  "StatusCode" : 429,
  "duration" : ""
})

and i no longer get the

2022-09-08 13:37:57.892 [ERROR] [main] [ChannelController.swift:741] [loadPreviousMessages(before:limit:completion:)] > You can't load new messages when there is no messages in the channel.

logs

going to try your bugfix/pagination-edge-case branch now

martinmitrevski commented 2 years ago

hey @AndrewSB,

I don't think that's the problem, since syncronize is also called in the view model, so I guess the error you get is because you call it too many times.

Please let us know how the branch works for you. If it doesn't help, we can have a call early next week to go through your issue.

Best, Martin

AndrewSB commented 2 years ago

yeah, i think this branch is working. @martinmitrevski can we whatever fix that is patched into the main version?

martinmitrevski commented 2 years ago

hey @AndrewSB, this is merged in the develop branch in our StreamChat repo. This week there's no release planned, so you can use it in the SwiftUI SDK by creating a branch pointing to StreamChat's develop branch.

AndrewSB commented 2 years ago

hey! we had this working on https://github.com/GetStream/stream-chat-swift/commit/9a39117a4677254a85464dbdcd4a4f4354e9792d, but after updating to https://github.com/GetStream/stream-chat-swift/commit/88f325da010642aa0e15452a1518164458d25190 it looks like the problem has re-appeared

nuno-vieira commented 2 years ago

HI @AndrewSB!

You are seeing again the "You can't load new messages when there are no messages in the channel." error log?

It is weird, that commit does not change anything related to this, theoretically at least.

Best, Nuno

AndrewSB commented 1 year ago

yup! sorry for the delay, got caught up with some other work

i just tested on https://github.com/GetStream/stream-chat-swift/commit/e729ddecab8424d0eb1b6e7f874dcd88a27c1b23, and i'm seeing those logs

2022-09-22 16:13:42.256 [ERROR] [main] [ChannelController.swift:741] [loadPreviousMessages(before:limit:completion:)] > You can't load new messages when there is no messages in the channel.
2022-09-22 16:13:42.280 [ERROR] [main] [ChannelController.swift:741] [loadPreviousMessages(before:limit:completion:)] > You can't load new messages when there is no messages in the channel.
2022-09-22 16:13:42.280 [ERROR] [main] [ChannelController.swift:741] [loadPreviousMessages(before:limit:completion:)] > You can't load new messages when there is no messages in the channel.
2022-09-22 16:13:42.280 [ERROR] [main] [ChannelController.swift:741] [loadPreviousMessages(before:limit:completion:)] > You can't load new messages when there is no messages in the channel.
2022-09-22 16:13:42.299 [ERROR] [main] [ChannelController.swift:741] [loadPreviousMessages(before:limit:completion:)] > You can't load new messages when there is no messages in the channel.
2022-09-22 16:13:42.299 [ERROR] [main] [ChannelController.swift:741] [loadPreviousMessages(before:limit:completion:)] > You can't load new messages when there is no messages in the channel.
2022-09-22 16:13:42.314 [ERROR] [main] [ChannelController.swift:741] [loadPreviousMessages(before:limit:completion:)] > You can't load new messages when there is no messages in the channel.
AndrewSB commented 1 year ago

testing with the latest now, just to make sure you haven't fixed it in the last 3 days (checking out https://github.com/GetStream/stream-chat-swift/commit/e6c14c8b05abf4024898cd78ee72303fa6def61f)

AndrewSB commented 1 year ago

it seems to work on the latest commit. was there a change that was made that you expected to fix it with? i'm wary of removing my version pin unless you believe you've made a change that actually solves this

polqf commented 1 year ago

The changes you are interested in should be the ones added in https://github.com/GetStream/stream-chat-swift/commit/233455651bb239c255f56f6f74d83fd4ba9a1a65 (PR: https://github.com/GetStream/stream-chat-swift/pull/2298)

AndrewSB commented 1 year ago

ahh awesome, cool. i think this is safe to close for now. we'll pin to that commit and take the next release that comes out. thanks @polqf!

polqf commented 1 year ago

Happy to hear that @AndrewSB 😄