Files
sodalive-ios/SodaLive/Sources/Content/Main/Banner/ContentMainBannerViewModel.swift
Yu Sung 8c58c08a85 perf(banner): TabView 프리로딩 완화·다운샘플링·요청취소 적용
배너/캐러셀에서 인접 페이지 프리로딩과 원본 해상도 디코딩으로
발생하던 메모리 스파이크와 중복 로드를 완화했습니다.

- 각 페이지에서 이미지 URL을 onAppear에 바인딩, onDisappear에 nil 해제
  → 인접 페이지 프리로딩 시 중복 로드·디코딩 방지, 요청 취소 실효
- 모든 KFImage에 cancelOnDisappear(true) 일관 적용
- 큰 배너 이미지에 downsampling(size:) 적용(디코딩 메모리 절감)
- 자동 슬라이드 주기 3초 → 4초로 완화(동시 로드 빈도 감소)
- TabView 페이지를 서브뷰로 분리하여 뷰 로직 단순화 및 재사용성 향상

결과: 동시 디코딩 감소, 피크 메모리 사용량 하락, 자동 슬라이드 안정성 개선
2025-10-23 15:31:52 +09:00

66 lines
2.4 KiB
Swift

//
// ContentMainBannerViewModel.swift
// SodaLive
//
// Created by klaus on 2023/12/11.
//
import Foundation
import Combine
final class ContentMainBannerViewModel: ObservableObject {
private let repository = ContentRepository()
private var subscription = Set<AnyCancellable>()
@Published var errorMessage = ""
@Published var isShowPopup = false
@Published var isLoading = false
@Published var currentIndex = 0
@Published var timer = Timer.publish(every: 4, on: .main, in: .common).autoconnect()
@Published var bannerList = [GetAudioContentBannerResponse]()
func getBannerList() {
isLoading = true
repository.getMainBannerList()
.sink { result in
switch result {
case .finished:
DEBUG_LOG("finish")
case .failure(let error):
ERROR_LOG(error.localizedDescription)
}
} receiveValue: { [unowned self] response in
self.isLoading = false
let responseData = response.data
do {
let jsonDecoder = JSONDecoder()
let decoded = try jsonDecoder.decode(ApiResponse<[GetAudioContentBannerResponse]>.self, from: responseData)
self.isLoading = false
if let data = decoded.data, decoded.success {
self.bannerList.removeAll()
self.bannerList.append(contentsOf: data)
} else {
if let message = decoded.message {
self.errorMessage = message
} else {
self.errorMessage = "배너를 불러오지 못했습니다. 다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
}
self.isShowPopup = true
}
} catch {
self.errorMessage = "배너를 불러오지 못했습니다. 다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
self.isShowPopup = true
self.isLoading = false
}
}
.store(in: &subscription)
}
}