Browse Questions
- Use @Binding to pass a two-way connection into the sheet, so changes inside the sheet reflect in the parent.
- OperationQueue is a higher-level abstraction over GCD.
- TSCellSeal(key:) uses the bytes you supply directly as the symmetric key, while TSCellSeal(passphrase:) runs a human-typed string through a deliberately slow key derivation function first.
- The Repository pattern abstracts the data layer behind a protocol.
Answer: The Repository pattern abstracts the data layer behind a protocol. The rest of the app doesn't know or care whether data comes from a network, database, or cache.
Code Example:
// Protocol — defines what the repo can do
protocol ArticleRepository {
func fetchAll() async throws -> [Article]
func fetch(id: String) async throws -> Article
func save(_ article: Article) async throws
}
// Real implementation — hits network + caches to CoreData
class RemoteArticleRepository: ArticleRepository {
func fetchAll() async throws -> [Article] {
let articles = try await api.getArticles()
cache.save(articles)
return articles
}
}
// Test implementation — returns fake data instantly
class MockArticleRepository: ArticleRepository {
func fetchAll() async throws -> [Article] {
return [Article.mock()]
}
}
// ViewModel uses the protocol — doesn't know which implementation it has
class ArticleViewModel: ObservableObject {
private let repo: ArticleRepository
init(repo: ArticleRepository) { self.repo = repo }
}