0
0
iOS Swiftmobile~20 mins

Repository pattern in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Repository Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main purpose of the Repository pattern in iOS development?

Choose the best explanation for why developers use the Repository pattern in iOS apps.

ATo speed up app launch by loading all data at once in the AppDelegate.
BTo separate data access logic from business logic, making code easier to maintain and test.
CTo directly connect UI elements to the database without any intermediate layer.
DTo replace the need for view controllers by handling UI rendering inside the repository.
Attempts:
2 left
💡 Hint

Think about how you keep your code organized and easy to change.

ui_behavior
intermediate
2:00remaining
How does using a Repository affect UI updates in an iOS app?

Consider an app where the UI shows a list of items fetched from a server. How does the Repository pattern help update the UI?

AThe Repository fetches data and notifies the UI to refresh only when data changes, avoiding unnecessary reloads.
BThe Repository forces the UI to reload every second regardless of data changes.
CThe Repository directly modifies UI elements without notifying the view controller.
DThe Repository disables UI updates until the app restarts.
Attempts:
2 left
💡 Hint

Think about efficient ways to keep the UI in sync with data.

lifecycle
advanced
2:00remaining
What happens if a Repository instance is not properly managed in an iOS app?

Consider a Repository that holds network connections and caches data. What is a likely problem if it is not released correctly?

AThe app will crash immediately on launch due to missing Repository instance.
BThe Repository will automatically delete all cached data when the app goes to background.
CMemory leaks occur because the Repository keeps strong references, causing the app to use more memory over time.
DThe Repository will convert all data to strings, causing data loss.
Attempts:
2 left
💡 Hint

Think about what happens when objects keep references and never get freed.

navigation
advanced
2:00remaining
How should a Repository be used when navigating between multiple view controllers in an iOS app?

Choose the best practice for sharing data managed by a Repository across different screens.

AInject the same Repository instance into each view controller to share data and keep it consistent.
BCreate a new Repository instance in every view controller to avoid data sharing.
CStore Repository data only in UserDefaults and reload it in each view controller.
DPass raw data arrays directly between view controllers without using a Repository.
Attempts:
2 left
💡 Hint

Think about how to keep data consistent and avoid duplication.

📝 Syntax
expert
2:00remaining
What is the output of this Swift code using a Repository pattern?

Consider this simplified Swift code snippet:

protocol UserRepository {
  func fetchUserName() -> String
}

class RemoteUserRepository: UserRepository {
  func fetchUserName() -> String {
    return "Alice"
  }
}

class UserViewModel {
  private let repository: UserRepository
  init(repository: UserRepository) {
    self.repository = repository
  }
  func displayName() -> String {
    return "User: \(repository.fetchUserName())"
  }
}

let repo = RemoteUserRepository()
let viewModel = UserViewModel(repository: repo)
print(viewModel.displayName())

What will be printed?

ACompilation error due to missing method implementation
BUser: RemoteUserRepository
CUser:
DUser: Alice
Attempts:
2 left
💡 Hint

Look at how the repository returns the user name and how the ViewModel uses it.