1

I have a collection view in my app, and it would refresh with animation when there is new content or deletion. However, I don't want it to refresh while user is scrolling because it would cause jerking. I want to refresh the collection view only when user has finished scrolling / when it's not scrolling.

So I have a data source driver, and I tried to use filter to make it wait till it becomes true but no luck.

This is my scrolling driver that I pass to the ViewModel

let isScrollViewScrollingDriver = Observable.merge(
            gridCollectionView.rx.willBeginDragging.map { _ in true },
            gridCollectionView.rx.didEndDragging.map { _ in false }
        ).asDriver(onErrorJustReturn: false).startWith(false).distinctUntilChanged()

my ViewModel init in view controller

viewModel = ViewModel(
            photoLibraryService: PhotoLibraryService.shared,
            isGridViewScrolling: isScrollViewScrollingDriver,
            disposeBag: disposeBag
        )

My ViewModel

let assetDriver = photoLibraryService.albumDriver.asObservable()
                .withLatestFrom(
                    isGridViewScrolling.asObservable().filter { $0 == false }
                ) { (arg0, arg1) in
                    return arg0
                }.flatMapLatest { libraryAlbum -> Observable<[LibraryAsset]> in
                    return photoLibraryService.convert(album: libraryAlbum)
                }.asDriver(onErrorJustReturn: []).startWith([]).distinctUntilChanged()

And then I map assetDriver to a dataSourceDriver that drive my collection view.

What changes can I make to the assetDriver to make it wait for isGridViewScrolling to become false? Thanks.

Henry Ngan
  • 572
  • 1
  • 4
  • 24
  • Try this answer I provided in .NET a few years back. Not used rx-swift so some of the operators have probably got different names, but I think its doing what you need: https://stackoverflow.com/questions/23431018/how-can-i-alternately-buffer-and-flow-a-live-data-stream-in-rx/23431077#23431077 – James World Feb 02 '20 at 08:53
  • Thanks!! I’ll try to translate it back to Swift to see if it works! – Henry Ngan Feb 02 '20 at 09:08

2 Answers2

1

It sounds like you need my stallUnless(_:initial:) operator. https://gist.github.com/danielt1263/2b624d7c925d8b7910ef2f1e5afe177b


    /**
     Emits values immediately if the boundary sequence last emitted true, otherwise collects elements from the source sequence until the boundary sequence emits true then emits the collected elements.
     - parameter boundary: Triggering event sequence.
     - parameter initial: The initial value of the boundary
     - returns: An Observable sequence.
     */
    func stallUnless<O>(_ boundary: O, initial: Bool) -> Observable<Element> where O: ObservableType, O.Element == Bool {
        return Observable.merge(self.map(Action.value), boundary.startWith(initial).distinctUntilChanged().materialize().map(Action.trigger).takeUntil(self.takeLast(1)))
            .scan((buffer: [Element](), trigger: initial, out: [Element]()), accumulator: { current, new in
                switch new {
                case .value(let value):
                    return current.trigger ? (buffer: [], trigger: current.trigger, out: [value]) : (buffer: current.buffer + [value], trigger: current.trigger, out: [])
                case .trigger(.next(let trigger)):
                    return trigger ? (buffer: [], trigger: trigger, out: current.buffer) : (buffer: current.buffer, trigger: trigger, out: [])
                case .trigger(.completed):
                    return (buffer: [], trigger: true, out: current.buffer)
                case .trigger(.error(let error)):
                    throw error
                }
            })
            .flatMap { $0.out.isEmpty ? Observable.empty() : Observable.from($0.out) }
    }
}
Daniel T.
  • 32,821
  • 6
  • 50
  • 72
0

You can use combineLatest:

    let assetDriver = Driver
        .combineLatest(
            photoLibraryService.albumDriver,
            isGridViewScrolling
        )
        .filter { !$1 }
        .map { $0.0 }
        .flatMapLatest { libraryAlbum -> Driver<[LibraryAsset]> in
            photoLibraryService.convert(album: libraryAlbum)
                .asDriver(onErrorJustReturn: [])
        }
        .startWith([])
        .distinctUntilChanged()
hell0friend
  • 561
  • 1
  • 3
  • 4