Bird
Raised Fist0
iOS Swiftmobile~20 mins

Repository pattern in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main purpose of the Repository pattern in iOS Swift development?

easy
A. To separate data access logic from the rest of the app
B. To handle UI animations smoothly
C. To manage app navigation between screens
D. To store user interface layout information

Solution

  1. Step 1: Understand the role of Repository pattern

    The Repository pattern is designed to isolate data access logic from business logic in an app.
  2. Step 2: Identify the correct purpose

    Separating data access makes the app easier to maintain and test, which matches To separate data access logic from the rest of the app.
  3. Final Answer:

    To separate data access logic from the rest of the app -> Option A
  4. Quick Check:

    Repository pattern = separate data access [OK]
Hint: Repository = data access separation [OK]
Common Mistakes:
  • Confusing Repository with UI or navigation roles
  • Thinking Repository manages animations
  • Assuming Repository stores UI layout
2.

Which of the following is the correct way to declare a repository protocol in Swift?

?
easy
A. struct UserRepository { func fetchUsers() -> [User] }
B. class UserRepository { func fetchUsers() -> [User] }
C. enum UserRepository { func fetchUsers() -> [User] }
D. protocol UserRepository { func fetchUsers() -> [User] }

Solution

  1. Step 1: Identify correct Swift syntax for protocols

    Protocols in Swift are declared with the keyword protocol followed by method signatures without bodies.
  2. Step 2: Match the correct declaration

    protocol UserRepository { func fetchUsers() -> [User] } correctly declares a protocol with a method signature. Other options use class, struct, or enum which are not protocols.
  3. Final Answer:

    protocol UserRepository { func fetchUsers() -> [User] } -> Option D
  4. Quick Check:

    Protocol declaration uses protocol keyword [OK]
Hint: Protocols start with 'protocol' keyword [OK]
Common Mistakes:
  • Using class or struct instead of protocol for interface
  • Adding method bodies inside protocol
  • Using enum incorrectly for repository
3.

Given the following code snippet, what will be printed?

protocol UserRepository {
  func fetchUsers() -> [String]
}

class MockUserRepository: UserRepository {
  func fetchUsers() -> [String] {
    return ["Alice", "Bob"]
  }
}

let repo: UserRepository = MockUserRepository()
print(repo.fetchUsers())
medium
A. []
B. ["Alice", "Bob"]
C. nil
D. Compilation error

Solution

  1. Step 1: Understand protocol conformance and method implementation

    The class MockUserRepository conforms to UserRepository and implements fetchUsers() returning ["Alice", "Bob"].
  2. Step 2: Analyze the print statement

    The variable repo is typed as UserRepository but holds an instance of MockUserRepository. Calling fetchUsers() returns ["Alice", "Bob"], which is printed.
  3. Final Answer:

    ["Alice", "Bob"] -> Option B
  4. Quick Check:

    Protocol method returns mock data [OK]
Hint: Protocol variable calls concrete implementation [OK]
Common Mistakes:
  • Expecting empty array instead of returned data
  • Thinking protocol variable can't call methods
  • Assuming compilation error due to protocol
4.

Identify the error in this repository implementation:

protocol UserRepository {
  func fetchUsers() -> [String]
}

class UserRepo: UserRepository {
  func fetchUsers() {
    return ["Alice", "Bob"]
  }
}
medium
A. Protocol method has wrong name
B. Method does not return a value
C. Missing return type in method signature
D. Class does not conform to protocol

Solution

  1. Step 1: Compare protocol and class method signatures

    The protocol method fetchUsers() returns [String]. The class method lacks the return type in its signature.
  2. Step 2: Identify the mismatch causing error

    Because the class method signature does not match the protocol, it causes a compilation error. The method must declare the return type.
  3. Final Answer:

    Missing return type in method signature -> Option C
  4. Quick Check:

    Method signatures must match protocol [OK]
Hint: Method signature must match protocol exactly [OK]
Common Mistakes:
  • Forgetting to add return type in implementation
  • Assuming method name mismatch
  • Thinking protocol conformance is automatic
5.

You want to switch your data source from a local database to a remote API without changing your app's business logic. How does the Repository pattern help you achieve this?

hard
A. By defining a repository interface and implementing separate classes for each data source
B. By embedding API calls directly in the UI code
C. By storing all data in global variables accessible everywhere
D. By using a single class that mixes local and remote data fetching

Solution

  1. Step 1: Understand the role of repository interface

    The Repository pattern uses an interface (protocol) to abstract data access methods.
  2. Step 2: Implement separate classes for each data source

    You create one class for local database access and another for remote API, both conforming to the same interface. This allows switching without changing business logic.
  3. Final Answer:

    By defining a repository interface and implementing separate classes for each data source -> Option A
  4. Quick Check:

    Interface + multiple implementations = easy data source switch [OK]
Hint: Use interface + multiple classes for data sources [OK]
Common Mistakes:
  • Mixing data fetching code in UI layer
  • Using global variables for data access
  • Combining local and remote logic in one class
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard