Created
January 23, 2018 11:25
-
-
Save MainasuK/d47ecf83a8b53bba2f5b8849d98a4746 to your computer and use it in GitHub Desktop.
RxSwift parallel downloading demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func startDownload() { | |
let eoCategories = EONET.categories | |
let downloadedEvents = eoCategories.flatMap { categories in | |
return Observable.from(categories.map { category in | |
EONET.events(forLast: 360, category: category) | |
}) | |
} | |
.merge(maxConcurrent: 2) | |
let updatedCategories = eoCategories.flatMap { categories in | |
downloadedEvents.scan(categories, accumulator: { (updated, events) in | |
return updated.map { category in | |
let eventForCategory = EONET.filteredEvents(events: events, forCategory: category) | |
if !eventForCategory.isEmpty { | |
var cat = category | |
cat.events = cat.events + eventForCategory | |
return cat | |
} else { | |
return category | |
} | |
} | |
}) | |
} | |
eoCategories | |
.concat(updatedCategories) | |
.bindTo(categories) | |
.addDisposableTo(disposeBag) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment