Fix array oob crash (IOS-257) (#1279)

Fix array oob crash
This commit is contained in:
Marcus Kida 2024-04-25 16:14:08 +02:00 committed by GitHub
parent ae02dd2994
commit ddb3211641
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 5 deletions

View File

@ -105,9 +105,10 @@ extension HomeTimelineViewModel.LoadLatestState {
guard let viewModel else { return } guard let viewModel else { return }
Task { @MainActor in
let latestFeedRecords = viewModel.dataController.records.prefix(APIService.onceRequestStatusMaxCount) let latestFeedRecords = viewModel.dataController.records.prefix(APIService.onceRequestStatusMaxCount)
Task {
let latestStatusIDs: [Status.ID] = latestFeedRecords.compactMap { record in let latestStatusIDs: [Status.ID] = latestFeedRecords.compactMap { record in
return record.status?.reblog?.id ?? record.status?.id return record.status?.reblog?.id ?? record.status?.id
} }
@ -128,7 +129,7 @@ extension HomeTimelineViewModel.LoadLatestState {
) )
} }
await enter(state: Idle.self) enter(state: Idle.self)
viewModel.receiveLoadingStateCompletion(.finished) viewModel.receiveLoadingStateCompletion(.finished)
// stop refresher if no new statuses // stop refresher if no new statuses
@ -146,7 +147,7 @@ extension HomeTimelineViewModel.LoadLatestState {
for (i, record) in newRecords.enumerated() { for (i, record) in newRecords.enumerated() {
if let index = oldRecords.firstIndex(where: { $0.status?.reblog?.id == record.id || $0.status?.id == record.id }) { if let index = oldRecords.firstIndex(where: { $0.status?.reblog?.id == record.id || $0.status?.id == record.id }) {
oldRecords[index] = record oldRecords[index] = record
if newRecords.count > index { if newRecords.count > i {
newRecords.remove(at: i) newRecords.remove(at: i)
} }
} }
@ -165,7 +166,7 @@ extension HomeTimelineViewModel.LoadLatestState {
} }
} catch { } catch {
await enter(state: Idle.self) enter(state: Idle.self)
viewModel.didLoadLatest.send() viewModel.didLoadLatest.send()
viewModel.receiveLoadingStateCompletion(.failure(error)) viewModel.receiveLoadingStateCompletion(.failure(error))
} }