Back to Rxswift

GroupedObservable

docs/Structs/GroupedObservable.html

6.10.23.6 KB
Original Source

GroupedObservable

public struct GroupedObservable<Key, Element> : ObservableType

Represents an observable sequence of elements that share a common key. GroupedObservable is typically created by the groupBy operator. Each GroupedObservable instance represents a collection of elements that are grouped by a specific key.

Example usage:

let observable = Observable.of("Apple", "Banana", "Apricot", "Blueberry", "Avocado")

let grouped = observable.groupBy { fruit in
    fruit.first! // Grouping by the first letter of each fruit
}

_ = grouped.subscribe { group in
    print("Group: \(group.key)")
    _ = group.subscribe { event in
        print(event)
    }
}

This will print:

Group: A
next(Apple)
next(Apricot)
next(Avocado)
Group: B
next(Banana)
next(Blueberry)

`

                key
                `

The key associated with this grouped observable sequence. All elements emitted by this observable share this common key.

Declaration

Swift

public let key: Key

`

                init(key:source:)
                `

Initializes a grouped observable sequence with a key and a source observable sequence.

Example usage:

let sourceObservable = Observable.of("Apple", "Apricot", "Avocado")
let groupedObservable = GroupedObservable(key: "A", source: sourceObservable)

_ = groupedObservable.subscribe { event in
    print(event)
}

This will print:

next(Apple)
next(Apricot)
next(Avocado)

Declaration

Swift

public init(key: Key, source: Observable<Element>)

Parameters

| key |

The key associated with this grouped observable sequence.

| | source |

The observable sequence of elements for the specified key.

|

`

                subscribe(_:)
                `

Subscribes an observer to receive events emitted by the source observable sequence.

Example usage:

let fruitsObservable = Observable.of("Apple", "Banana", "Apricot", "Blueberry", "Avocado")
let grouped = fruitsObservable.groupBy { $0.first! } // Group by first letter

_ = grouped.subscribe { group in
    if group.key == "A" {
        _ = group.subscribe { event in
            print(event)
        }
    }
}

This will print:

next(Apple)
next(Apricot)
next(Avocado)

Declaration

Swift

public func subscribe<Observer>(_ observer: Observer) -> Disposable where Element == Observer.Element, Observer : ObserverType

Parameters

| observer |

The observer that will receive the events of the source observable.

|

Return Value

A Disposable representing the subscription, which can be used to cancel the subscription.

`

                asObservable()
                `

Converts this GroupedObservable into a regular Observable sequence. This allows you to work with the sequence without directly interacting with the key.

Example usage:

let fruitsObservable = Observable.of("Apple", "Banana", "Apricot", "Blueberry", "Avocado")
let grouped = fruitsObservable.groupBy { $0.first! } // Group by first letter

_ = grouped.subscribe { group in
    if group.key == "A" {
        let regularObservable = group.asObservable()
        _ = regularObservable.subscribe { event in
            print(event)
        }
    }
}

This will print:

next(Apple)
next(Apricot)
next(Avocado)

Declaration

Swift

public func asObservable() -> Observable<Element>

Return Value

The underlying Observable sequence of elements for the specified key.