Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Commonmark migration
Source Link

I hope this is helpful. I found [a blogpost][1]a blogpost that talks about doing stuff onAppear for a navigation view.

Idea would be that you bake your service into a BindableObject and subscribe to those updates in your view.

struct SearchView : View { @State private var query: String = "Swift" @EnvironmentObject var repoStore: ReposStore var body: some View { NavigationView { List { TextField($query, placeholder: Text("type something..."), onCommit: fetch) ForEach(repoStore.repos) { repo in RepoRow(repo: repo) } }.navigationBarTitle(Text("Search")) }.onAppear(perform: fetch) } private func fetch() { repoStore.fetch(matching: query) } } 
import SwiftUI import Combine class ReposStore: BindableObject { var repos: [Repo] = [] { didSet { didChange.send(self) } } var didChange = PassthroughSubject<ReposStore, Never>() let service: GithubService init(service: GithubService) { self.service = service } func fetch(matching query: String) { service.search(matching: query) { [weak self] result in DispatchQueue.main.async { switch result { case .success(let repos): self?.repos = repos case .failure: self?.repos = [] } } } } } 

Credit to: Majid Jabrayilov [1]: https://mecid.github.io/2019/06/05/swiftui-making-real-world-app/

I hope this is helpful. I found [a blogpost][1] that talks about doing stuff onAppear for a navigation view.

Idea would be that you bake your service into a BindableObject and subscribe to those updates in your view.

struct SearchView : View { @State private var query: String = "Swift" @EnvironmentObject var repoStore: ReposStore var body: some View { NavigationView { List { TextField($query, placeholder: Text("type something..."), onCommit: fetch) ForEach(repoStore.repos) { repo in RepoRow(repo: repo) } }.navigationBarTitle(Text("Search")) }.onAppear(perform: fetch) } private func fetch() { repoStore.fetch(matching: query) } } 
import SwiftUI import Combine class ReposStore: BindableObject { var repos: [Repo] = [] { didSet { didChange.send(self) } } var didChange = PassthroughSubject<ReposStore, Never>() let service: GithubService init(service: GithubService) { self.service = service } func fetch(matching query: String) { service.search(matching: query) { [weak self] result in DispatchQueue.main.async { switch result { case .success(let repos): self?.repos = repos case .failure: self?.repos = [] } } } } } 

Credit to: Majid Jabrayilov [1]: https://mecid.github.io/2019/06/05/swiftui-making-real-world-app/

I hope this is helpful. I found a blogpost that talks about doing stuff onAppear for a navigation view.

Idea would be that you bake your service into a BindableObject and subscribe to those updates in your view.

struct SearchView : View { @State private var query: String = "Swift" @EnvironmentObject var repoStore: ReposStore var body: some View { NavigationView { List { TextField($query, placeholder: Text("type something..."), onCommit: fetch) ForEach(repoStore.repos) { repo in RepoRow(repo: repo) } }.navigationBarTitle(Text("Search")) }.onAppear(perform: fetch) } private func fetch() { repoStore.fetch(matching: query) } } 
import SwiftUI import Combine class ReposStore: BindableObject { var repos: [Repo] = [] { didSet { didChange.send(self) } } var didChange = PassthroughSubject<ReposStore, Never>() let service: GithubService init(service: GithubService) { self.service = service } func fetch(matching query: String) { service.search(matching: query) { [weak self] result in DispatchQueue.main.async { switch result { case .success(let repos): self?.repos = repos case .failure: self?.repos = [] } } } } } 

Credit to: Majid Jabrayilov

Source Link
andromedainiative
  • 5.1k
  • 6
  • 24
  • 34

I hope this is helpful. I found [a blogpost][1] that talks about doing stuff onAppear for a navigation view.

Idea would be that you bake your service into a BindableObject and subscribe to those updates in your view.

struct SearchView : View { @State private var query: String = "Swift" @EnvironmentObject var repoStore: ReposStore var body: some View { NavigationView { List { TextField($query, placeholder: Text("type something..."), onCommit: fetch) ForEach(repoStore.repos) { repo in RepoRow(repo: repo) } }.navigationBarTitle(Text("Search")) }.onAppear(perform: fetch) } private func fetch() { repoStore.fetch(matching: query) } } 
import SwiftUI import Combine class ReposStore: BindableObject { var repos: [Repo] = [] { didSet { didChange.send(self) } } var didChange = PassthroughSubject<ReposStore, Never>() let service: GithubService init(service: GithubService) { self.service = service } func fetch(matching query: String) { service.search(matching: query) { [weak self] result in DispatchQueue.main.async { switch result { case .success(let repos): self?.repos = repos case .failure: self?.repos = [] } } } } } 

Credit to: Majid Jabrayilov [1]: https://mecid.github.io/2019/06/05/swiftui-making-real-world-app/