class GitHubViewModel: ObservableObject { private let gitHubAPIClient: GitHubAPIClient private var cancellables: Set<AnyCancellable> = [] @Published var searchWord = "" @Published var repositories: [GitHubRepository] = [] init(gitHubAPIClient: GitHubAPIClient) { self.gitHubAPIClient = gitHubAPIClient } func searchButtonTapped() { gitHubAPIClient .searchRepository(searchWord) // Publishing changes from background threads is not allowed; // make sure to publish values from the main thread (via operators like receive(on:)) on model updates. .sink { [weak self] in self?.repositories = $0.items } .store(in: &cancellables) } }
final class MyTestScheduler<SchedulerTimeType, SchedulerOptions> { // ... func advance(by stride: SchedulerTimeType.Stride = .zero) { now = now.advanced(by: stride) var index = 0 // index で scheduled の配列の先頭から走査していくことによって、 // interval action により scheduled に追加されていく action にも対応できる while index < scheduled.count { let work = scheduled[index] if work.date <= now { work.action() scheduled.remove(at: index) } else { index += 1 } } } }
SchedulerOptions> { func advance(by stride: SchedulerTimeType.Stride = .zero) { // 即座に指定された最終的な値まで now を進めてしまっている now = now.advanced(by: stride) var index = 0 while index < scheduled.count { let work = scheduled[index] if work.date <= now { work.action() scheduled.remove(at: index) } else { index += 1 } } } }