# onRecommendation(for:onError:_:)

> Calls handler now (once known) and on every change: signing in, a new body, a simulation finishing.

Source: https://docs.tryyoursize.com/reference/ios/on-recommendation/

*Method* · `TryYourSize (Swift)`

Calls `handler` now (once known) and on every change: signing in, a new body, a simulation finishing. Keep the subscription; `cancel()` it when done.

```swift
@discardableResult
public func onRecommendation(for product: String, onError: ((TryYourSizeError) -> Void)? = nil,
                                 _ handler: @escaping (Recommendation) -> Void) -> TYSSubscription
```

## Parameters

- **`for`** (`String`, required): Your product ID or SKU.
- **`onError`** (`((TryYourSizeError) -> Void)?`, optional, default `nil`): Called when our window cannot be reached or the app is not registered.
- **`handler`** (`@escaping (Recommendation) -> Void`, required): Called now (once known) and on every change.

## Returns

A subscription: keep it, and `cancel()` it when you are done.

## Example

**Swift**

```swift
let sub = tys.onRecommendation(for: "SKU-123") { rec in
    if rec.status == .ready, !touched { selectedSize = rec.size }
}
// keep `sub`; sub.cancel() when the screen goes away
```

**Kotlin**

```kotlin
val sub = tys.onRecommendation("SKU-123") { rec ->
    if (rec.status == Recommendation.Status.READY && !touched) selected = rec.size
}
// sub.cancel() in onDestroy
```

**React Native**

```tsx
useEffect(() => {
  const sub = addRecommendationListener("SKU-123", (rec) => {
    if (rec.status === "ready" && !touched) setSize(rec.size);
  });
  return () => sub.remove();
}, []);
```

**Flutter**

```dart
final sub = tys.onRecommendation('SKU-123').listen((rec) {
  if (rec.status == RecommendationStatus.ready && !touched) setState(() => size = rec.size);
});
// sub.cancel() in dispose()
```
