Skip to content

Instantly share code, notes, and snippets.

@MainasuK
Created January 23, 2018 11:25
Show Gist options
  • Save MainasuK/d47ecf83a8b53bba2f5b8849d98a4746 to your computer and use it in GitHub Desktop.
Save MainasuK/d47ecf83a8b53bba2f5b8849d98a4746 to your computer and use it in GitHub Desktop.
RxSwift parallel downloading demo
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