Created
November 7, 2017 17:49
-
-
Save worthbak/b401c0652e61d6fc7d0347ac94c23bae to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /// Using `blockURLComponent`, requests relenvant data from FRING API for the given blocks. Before | |
| /// requesting, it will check the `fetchedBlockIds` set, attempting to avoid requesting duplicate | |
| /// information. After data is fetched, `processOperation` will be called. | |
| func requestData(for blockIds: [(MKShape, String)]) -> Observable<[[String: Any]]> { | |
| assert(parse != nil, "parse closure was never set; this is a requirement of use") | |
| let rv = PublishSubject<[[String: Any]]>() | |
| // bail if we already have all block data | |
| let filteredBlockIds = blockIds | |
| .flatMap { _, blockId -> String? in | |
| if self.fetchedBlockIds.contains(blockId) { | |
| return nil | |
| } else { | |
| return blockId | |
| } | |
| } | |
| guard filteredBlockIds.count > 0 else { | |
| rv.onCompleted() | |
| return rv | |
| } | |
| let bodies = prepareHttpBodies(from: filteredBlockIds) | |
| if bodies.count == 1 { | |
| // not relevant | |
| // ... | |
| } else if bodies.count > 1 { | |
| queue.async { [rv] in | |
| let group = DispatchGroup() | |
| let innerRv = PublishSubject<[[String: Any]]>() | |
| let innerDisposable = innerRv | |
| .reduce([], accumulator: +) | |
| .observeOn(MainScheduler.instance) | |
| .subscribe(onNext: { final in | |
| Logger.verbose("processing \(final.count) pieces of data") | |
| self.process(data: final) | |
| rv.onNext(final) | |
| rv.onCompleted() | |
| }) | |
| let nextQueue = DispatchQueue(label: "com.housecanary.HouseCanary:BlockProcessor:Next") | |
| bodies.forEach { self.makeGroupedRequest(with: $0, subject: innerRv, group: group, nextQueue: nextQueue) } | |
| group.wait() | |
| innerRv.onCompleted() | |
| innerDisposable.dispose() | |
| innerRv.dispose() | |
| } | |
| } | |
| return rv | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment