Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
retreive post fav reactions with rxswift
let userInfo = reloadTrigger
.flatMapLatest { [apiService, selectedUser] in
apiService.fetchUserInfo(with: selectedUser.id)
.trackActivity(activityIndicator)
}
.share(replay: 1)
let favorites = userInfo
.flatMapLatest { [apiService] in
apiService.fetchFavorites(with: $0.id)
.trackActivity(activityIndicator)
}
.share(replay: 1)
let posts = userInfo
.flatMapLatest { [apiService] in
apiService.fetchPosts(with: $0.id)
.trackActivity(activityIndicator)
}
.share(replay: 1)
let reactions = posts.map { $0.filter { $0.reactionCount > 0 } }
.reduce([]) { $0 + $1 }
.map { $0.map { $0.id } }
.flatMapLatest { [apiService] in
apiService.fetchReactions(with: $0)
.trackActivity(activityIndicator)
}
.share(replay: 1)
let userInfoSave = userInfo.do(onNext: { [realmController] info in
try info.addUserInfo(to: realmController)
})
let favoritesSave = favorites.do(onNext: { [realmController] favorites in
try favorites.addFavorites(to: realmController)
})
let postsSave = posts.do(onNext: { [realmController] posts in
try posts.addUserPosts(to: realmController)
})
let reactionsSave = reactions.do(onNext: { [realmController] reactions, postId in
try reactions.addReactions(with: postId, realmController: realmController)
})
Observable.combineLatest(userInfoSave, favoritesSave, postsSave, reactionsSave)
.subscribe()
.disposed(by: bag)
reloadTrigger.accept(())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment