简体   繁体   中英

How to reload data in SwiftUI List?

I'm new to iOS dev, so sorry if it's an obvious question. But I can't figure out how to update the data in SwiftUI List. I'm fetching the data from API and using @ObservedObject to pass it to the ContentView. It works fine when I'm launching my app, but after I change my API request (by typing a keyword in the SearchBar) and fetch it again, it doesn't seem to update the List, even though the data was changed.

ContentView.swift

struct ContentView: View {

@ObservedObject var networkManager = NetworkManager()

@State var searchText: String = ""

var body: some View {
    NavigationView{
        VStack {
            SearchBar(text: $searchText, placeholder: "Enter a keyword")
            List(networkManager.posts) { post in
                NavigationLink(destination: DetailView(url: post.url)) {
                    HStack {
                        Text(post.title)
                    }
                }
                
            }.gesture(DragGesture().onChanged { _ in UIApplication.shared.endEditing() })
            
        }.navigationBarTitle("News")
        
    }
    .onAppear {
        self.networkManager.fetchData(self.searchText)
    }
    
}
}

NetworkManager.swift

class NetworkManager: ObservableObject {



@Published var posts = [Post]()

func fetchData(_ keyword: String?){
    var urlString = "https://newsapi.org/v2/top-headlines?country=us&apiKey=5dcef32f4c69413e8fe128cc5c7ba4cf"
    if keyword != nil {
        urlString = "https://newsapi.org/v2/top-headlines?country=us&apiKey=5dcef32f4c69413e8fe128cc5c7ba4cf&q=\(keyword!)"
    }
    print(urlString)
    if let url = URL(string: urlString){
        let session = URLSession(configuration: .default)
        let task = session.dataTask(with: url) { (data, response, error) in
            if error == nil{
                let decoder = JSONDecoder()
                if let safeData = data{
                    do{
                        let results = try decoder.decode(News.self, from: safeData)
                        DispatchQueue.main.async {
                            self.posts = results.articles
                            print(self.posts)
                        }
                    } catch{
                        print(error)
                    }
                }
            }
        }
        task.resume()
    }
    
}

}

SearchBar.swift (I fetch data again inside searchBarSearchButtonClicked)

struct SearchBar: UIViewRepresentable {
    
    
    @Binding var text: String
    var placeholder: String
    
    class Coordinator: NSObject, UISearchBarDelegate {
       
        @ObservedObject var networkManager = NetworkManager()
        
        @Binding var text: String
        
        init(text: Binding<String>) {
            _text = text
        }
        
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            text = searchText
        }
        
        func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
            print(text)
            DispatchQueue.main.async {
                self.networkManager.fetchData(self.text)
            }
            
            UIApplication.shared.endEditing()
            
        }
    }
    
    func makeCoordinator() -> SearchBar.Coordinator {
        return Coordinator(text: $text)
    }
    
    func makeUIView(context: UIViewRepresentableContext<SearchBar>) -> UISearchBar {
        let searchBar = UISearchBar(frame: .zero)
        searchBar.delegate = context.coordinator
        searchBar.placeholder = placeholder
        searchBar.searchBarStyle = .minimal
        searchBar.autocapitalizationType = .none
        return searchBar
    }
    
    func updateUIView(_ uiView: UISearchBar, context: UIViewRepresentableContext<SearchBar>) {
        uiView.text = text
    }
    
}

extension UIApplication {
    func endEditing() {
        sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

News.swift

struct News: Decodable {
    let articles: [Post]
}

struct Post: Decodable, Identifiable {
    var id: String{
        return url!
    }
    let title: String
    let url: String?
}

I've made a few minor modifications and made the code work in Xcode-playgrounds. Here's how:

Model:

struct News: Codable { var articles: [Post] }
struct Post: Identifiable, Codable { var title: String; var id: String { title } }

ContentView:

struct ContentView: View {

    @ObservedObject var networkManager = NetworkManager()

    var body: some View {
        NavigationView {
            VStack {
                TextField("Enter a keyword", text: $networkManager.searchText)
                List(networkManager.posts) { post in
                    NavigationLink(destination: EmptyView()) {
                        HStack {
                            Text(post.title)
                        }
                    }
                }
            }.navigationBarTitle("News")
        }
        .onAppear {
            self.networkManager.fetchData()
        }

    }
}

NetworkManager:

class NetworkManager: ObservableObject {

    @Published var searchText: String = "" {
        didSet {
            fetchData()
        }
    }
    @Published var posts = [Post]()

    func fetchData() {
        let urlString = "https://newsapi.org/v2/top-headlines?country=us&apiKey=5dcef32f4c69413e8fe128cc5c7ba4cf&q=\(searchText)"
        if let url = URL(string: urlString){
            let session = URLSession(configuration: .default)
            let task = session.dataTask(with: url) { (data, response, error) in
                if error == nil{
                    let decoder = JSONDecoder()
                    if let safeData = data{
                        do{
                            let results = try decoder.decode(News.self, from: safeData)
                            DispatchQueue.main.async {
                                self.posts = results.articles
                                print(self.posts)
                            }
                        } catch{
                            print(error)
                        }
                    }
                }
            }
            task.resume()
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM