Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[홈 화면 버그] cancellable이 동시접근되는 이슈 #327

Merged
merged 1 commit into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions BBus/BBus/Foreground/Home/Model/HomeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ struct HomeFavoriteList {
self.changedByTimer = false
}

init(favorites: [HomeFavorite]) {
var orderedFavorites = [HomeFavorite]()
favorites.forEach { favorite in
if let index = orderedFavorites.firstIndex(where: { $0.stationId == favorite.stationId }) {
orderedFavorites[index].buses.append(contentsOf: favorite.buses)
}
else {
orderedFavorites.append(favorite)
}
}
self.favorites = orderedFavorites
self.changedByTimer = false
}

func count() -> Int {
return self.favorites.count
}
Expand Down Expand Up @@ -61,11 +75,11 @@ struct HomeFavoriteList {
}
}

struct HomeFavorite: Equatable {
typealias HomeFavoriteInfo = (favoriteItem: FavoriteItemDTO, arriveInfo: HomeArriveInfo?)

typealias HomeBusInfo = (favoriteItem: FavoriteItemDTO, arriveInfo: HomeArriveInfo?)
struct HomeFavorite: Equatable {

subscript(index: Int) -> HomeBusInfo? {
subscript(index: Int) -> HomeFavoriteInfo? {
guard 0..<self.buses.count ~= index else { return nil }
return self.buses[index]
}
Expand All @@ -76,7 +90,7 @@ struct HomeFavorite: Equatable {

let stationId: String
let arsId: String
var buses: [HomeBusInfo]
var buses: [HomeFavoriteInfo]

init(stationId: String, arsId: String, buses: [FavoriteItemDTO]) {
self.stationId = stationId
Expand Down
9 changes: 6 additions & 3 deletions BBus/BBus/Foreground/Home/UseCase/HomeAPIUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protocol HomeAPIUsable: BaseUseCase {
typealias HomeUseCases = GetFavoriteItemListUsable & CreateFavoriteItemUsable & GetStationListUsable & GetRouteListUsable & GetArrInfoByRouteListUsable

func fetchFavoriteData() -> AnyPublisher<[FavoriteItemDTO], Error>
func fetchBusRemainTime(favoriteItem: FavoriteItemDTO) -> AnyPublisher<ArrInfoByRouteDTO, Error>
func fetchBusRemainTime(favoriteItem: FavoriteItemDTO) -> AnyPublisher<HomeFavoriteInfo, Error>
func fetchStation() -> AnyPublisher<[StationDTO], Error>
func fetchBusRoute() -> AnyPublisher<[BusRouteDTO], Error>
}
Expand All @@ -35,15 +35,18 @@ final class HomeAPIUseCase: HomeAPIUsable {
.eraseToAnyPublisher()
}

func fetchBusRemainTime(favoriteItem: FavoriteItemDTO) -> AnyPublisher<ArrInfoByRouteDTO, Error> {
func fetchBusRemainTime(favoriteItem: FavoriteItemDTO) -> AnyPublisher<HomeFavoriteInfo, Error> {
return self.useCases.getArrInfoByRouteList(stId: favoriteItem.stId,
busRouteId: favoriteItem.busRouteId,
ord: favoriteItem.ord)
.decode(type: ArrInfoByRouteResult.self, decoder: JSONDecoder())
.tryMap({ item in
let result = item.msgBody.itemList
guard let item = result.first else { throw BBusAPIError.wrongFormatError }
return item
let homeFavoriteInfo: HomeFavoriteInfo
homeFavoriteInfo.favoriteItem = favoriteItem
homeFavoriteInfo.arriveInfo = HomeArriveInfo(arrInfoByRouteDTO: item)
return homeFavoriteInfo
})
.eraseToAnyPublisher()
}
Expand Down
39 changes: 27 additions & 12 deletions BBus/BBus/Foreground/Home/ViewModel/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,28 @@ final class HomeViewModel {
.store(in: &self.cancellables)
}

func loadRemainTime(with favoriteItems: [FavoriteItemDTO]) {
favoriteItems.forEach { [weak self] favoriteItem in
guard let self = self else { return }
self.apiUseCase.fetchBusRemainTime(favoriteItem: favoriteItem)
private func loadRemainTime(with favoriteItems: [FavoriteItemDTO]) {
guard let homeFavoriteList = homeFavoriteList else { return }

var newHomeFavoriteList = homeFavoriteList
favoriteItems.publisher
.receive(on: DispatchQueue.global())
.flatMap({ [weak self] (favoriteItem) -> AnyPublisher<HomeFavoriteInfo, Error> in
guard let self = self else { return NetworkError.unknownError.publisher }
return self.apiUseCase.fetchBusRemainTime(favoriteItem: favoriteItem)
})
.catchError({ [weak self] error in
self?.networkError = error
})
.map({ arrInfoByRouteDTO in
return HomeArriveInfo(arrInfoByRouteDTO: arrInfoByRouteDTO)
})
.sink(receiveValue: { [weak self] homeArrivalInfo in
guard let indexPath = self?.homeFavoriteList?.indexPath(of: favoriteItem) else { return }
self?.homeFavoriteList?.configure(homeArrivalinfo: homeArrivalInfo, indexPath: indexPath)
.map({ (homeFavoriteInfo) -> HomeFavoriteList? in
guard let indexPath = newHomeFavoriteList.indexPath(of: homeFavoriteInfo.favoriteItem),
let arriveInfo = homeFavoriteInfo.arriveInfo else { return nil }
newHomeFavoriteList.configure(homeArrivalinfo: arriveInfo, indexPath: indexPath)
return newHomeFavoriteList
})
.store(in: &self.cancellables)
}
.compactMap({ $0 })
.last()
.assign(to: &self.$homeFavoriteList)
}

private func loadBusRouteList() {
Expand Down Expand Up @@ -107,3 +113,12 @@ final class HomeViewModel {
return self.calculateUseCase.findBusType(in: self.busRouteList, by: busName)
}
}

fileprivate extension Error {
var publisher: AnyPublisher<HomeFavoriteInfo, Error> {
let publisher = CurrentValueSubject<HomeFavoriteInfo?, Error>(nil)
publisher.send(completion: .failure(self))
return publisher.compactMap({$0})
.eraseToAnyPublisher()
}
}