Challenge - 5 Problems
MVVM Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Understanding MVVM Roles
In the MVVM pattern, which component is responsible for holding the UI logic and preparing data for display?
Attempts:
2 left
💡 Hint
Think about which part acts as a bridge between data and UI.
✗ Incorrect
The ViewModel holds UI logic and prepares data from the Model to be displayed by the View. It acts as a bridge between Model and View.
❓ ui_behavior
intermediate2:00remaining
View Updates in MVVM
In an MVVM iOS app using SwiftUI, when the ViewModel updates a @Published property, what happens to the View?
Attempts:
2 left
💡 Hint
Consider how SwiftUI reacts to changes in @Published properties.
✗ Incorrect
SwiftUI Views observe @Published properties in the ViewModel and automatically refresh when these properties change.
❓ lifecycle
advanced2:00remaining
ViewModel Lifecycle Management
Which of the following is the best practice to avoid memory leaks when a ViewModel holds a reference to a ViewController in MVVM on iOS?
Attempts:
2 left
💡 Hint
Think about how to prevent retain cycles between ViewModel and ViewController.
✗ Incorrect
Using a weak reference prevents retain cycles and memory leaks when the ViewModel references the ViewController.
advanced
2:00remaining
Navigation Handling in MVVM
In MVVM, where should navigation logic (like pushing a new screen) ideally be handled to keep the ViewModel testable and free of UIKit dependencies?
Attempts:
2 left
💡 Hint
Consider separation of concerns and testability.
✗ Incorrect
Navigation should be handled by the View layer in response to signals or state changes from the ViewModel, keeping the ViewModel free of UIKit dependencies.
🔧 Debug
expert3:00remaining
Debugging MVVM Data Binding Issue
Given this SwiftUI ViewModel code snippet, why does the View not update when 'count' changes?
class CounterViewModel: ObservableObject {
var count = 0
func increment() {
count += 1
}
}
What is the cause?
Attempts:
2 left
💡 Hint
Check how SwiftUI detects changes in ViewModel properties.
✗ Incorrect
Without @Published, SwiftUI does not observe changes to 'count', so the View does not update.