Skip to content

Commit c1e60ac

Browse files
committed
Make mainViewModel lazy on iOS
This commit changes the initialization of `mainViewModel` in `IOSViewModelOwner.kt` to be lazy. This ensures that the `MainViewModel` is only created when it is first accessed.
1 parent 4fd84cc commit c1e60ac

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

Fruitties/shared/src/iosMain/kotlin/com/example/fruitties/di/viewmodel/IOSViewModelOwner.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ class IOSViewModelOwner(
1717
override val viewModelStore: ViewModelStore = ViewModelStore()
1818

1919
// Create an instance of MainViewModel with the CreationExtras.
20-
val mainViewModel: MainViewModel = ViewModelProvider.create(
21-
owner = this as ViewModelStoreOwner,
22-
factory = MainViewModel.Factory,
23-
extras = MainViewModel.newCreationExtras(appContainer),
24-
)[MainViewModel::class]
20+
val mainViewModel: MainViewModel by lazy {
21+
ViewModelProvider.create(
22+
owner = this as ViewModelStoreOwner,
23+
factory = MainViewModel.Factory,
24+
extras = MainViewModel.newCreationExtras(appContainer),
25+
)[MainViewModel::class]
26+
}
2527

2628
// To add more ViewModel types, add new properties for each ViewModel.
2729
// If we need to add a very large number of ViewModel types,

0 commit comments

Comments
 (0)