# InvalidationStrategySpecification

Artifact: [androidx.constraintlayout:constraintlayout-compose](https://developer.android.com/jetpack/androidx/releases/constraintlayout) [View Source](https://cs.android.com/search?q=file:androidx/constraintlayout/compose/MotionLayout.kt+class:androidx.constraintlayout.compose.InvalidationStrategySpecification) Added in [1.1.2](https://developer.android.com/jetpack/androidx/releases/constraintlayout#1.1.2)


```
class InvalidationStrategySpecification
```

<br />

*** ** * ** ***

Helper scope that provides some strategies to improve performance based on incoming constraints.

As a starting approach, we recommend trying the following:

```kotlin
MotionLayout(
    ...,
    invalidationStrategy = remember {
        InvalidationStrategy(
            onIncomingConstraints = { old, new ->
                // We invalidate every third frame, or when the change is higher than 5 pixels
                shouldInvalidateOnFixedWidth(old, new, skipCount = 3, threshold = 5) ||
                    shouldInvalidateOnFixedHeight(old, new, skipCount = 3, threshold = 5)
            },
            onObservedStateChange = null // Default behavior
        )
}
) {
   // content
}
```

See either `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedWidth(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)` or `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedHeight(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)` to learn more about the intent behind rate-limiting invalidation.

## Summary

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedHeight(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)( oldConstraints: https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Constraints, newConstraints: https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Constraints, skipCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, threshold: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Limits the rate at which MotionLayout is invalidated while `https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Constraints#hasFixedHeight()` is true. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedWidth(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)( oldConstraints: https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Constraints, newConstraints: https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Constraints, skipCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, threshold: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Limits the rate at which MotionLayout is invalidated while `https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Constraints#hasFixedWidth()` is true. |

## Public functions

### shouldInvalidateOnFixedHeight

```
fun shouldInvalidateOnFixedHeight(
    oldConstraints: Constraints,
    newConstraints: Constraints,
    skipCount: Int,
    threshold: Int
): Boolean
```

Limits the rate at which MotionLayout is invalidated while `https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Constraints#hasFixedHeight()` is true.

The rate limit is defined by two variables. Use `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedHeight(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)` to indicate how many consecutive measure passes should skip invalidation, you may then provide a `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedHeight(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)` (in pixels) to indicate when to invalidate regardless of how many passes are left to skip. This is important since you only want to skip invalidation passes when there's **not** a significant change in dimensions.

Overall, you don't want `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedHeight(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)` to be too high otherwise it'll result in a "jumpy" layout behavior, but you also don't want the `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedHeight(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)` to be too low, otherwise you'll lose the benefit of rate limiting.

A good starting point is setting `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedHeight(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)` to 3 and `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedHeight(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)` to 5. You can then adjust based on your expectations of performance and perceived smoothness.

### shouldInvalidateOnFixedWidth

```
fun shouldInvalidateOnFixedWidth(
    oldConstraints: Constraints,
    newConstraints: Constraints,
    skipCount: Int,
    threshold: Int
): Boolean
```

Limits the rate at which MotionLayout is invalidated while `https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Constraints#hasFixedWidth()` is true.

The rate limit is defined by two variables. Use `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedWidth(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)` to indicate how many consecutive measure passes should skip invalidation, you may then provide a `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedWidth(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)` (in pixels) to indicate when to invalidate regardless of how many passes are left to skip. This is important since you only want to skip invalidation passes when there's **not** a significant change in dimensions.

Overall, you don't want `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedWidth(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)` to be too high otherwise it'll result in a "jumpy" layout behavior, but you also don't want the `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedWidth(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)` to be too low, otherwise you'll lose the benefit of rate limiting.

A good starting point is setting `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedWidth(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)` to 3 and `https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/InvalidationStrategySpecification#shouldInvalidateOnFixedWidth(androidx.compose.ui.unit.Constraints,androidx.compose.ui.unit.Constraints,kotlin.Int,kotlin.Int)` to 5. You can then adjust based on your expectations of performance and perceived smoothness.