0

父视图包含SearchBarand ForEach List,列表中的每个项目都包含在NavigationLink项目内,问题是父列表。NavigationLink点击inside后清除ForEach,我也添加UUID()了 for ForEach,但同样的问题发生了。我正在使用 Xcode 12.4,同样的问题发生在 iOS 13 和 14 上。

搜索视图

import SwiftUI

struct SearchView: View {
  @ObservedObject var simpleSearchMV = SimpleSearchModelView()

  var body: some View {
    VStack{
      SearchBar(searchTerm: self.$simpleSearchMV.searchText)
        .padding(.vertical, 10)
      ScrollView(showsIndicators: false) {
        ForEach(simpleSearchMV.dataResult.result ?? [], id: \.self.Id) { item in
          NavigationLink(destination: OffersView()) {
            Text(item.currentName)
          }

          Divider()
        }
        .id(UUID())
      }
    }
  }
}

简单搜索模型视图

import Foundation
import Combine
import SwiftUI
import Alamofire

class SimpleSearchModelView: ObservableObject, Identifiable {

  @ObservedObject var monitor = NetworkMonitor()

  @Published var dataResult: DataResult<[SearchSimpleResult]> = DataResult(loading: false)
  @Published var searchText: String = String()

  private var subscription: Set<AnyCancellable> = []

  init() {
    self.setSearchText()
  }

  func setSearchText() {
    $searchText
      .debounce(for: .milliseconds(500), scheduler: RunLoop.main) // debounces the string publisher, such that it delays the process of sending request to remote server.
      .removeDuplicates()
      .map({ (string) -> String? in
        if string.count < 1 {
          // self.dataResult.setData([])
          return nil
        }
        return string
      }) // prevents sending numerous requests and sends nil if the count of the characters is less than 1.
      .compactMap{ $0 } // removes the nil values so the search string does not get passed down to the publisher chain
      .sink { (_) in
        //
      } receiveValue: { [self] (searchField) in
        searchItems(searchText: searchField)
      }.store(in: &subscription)
  }

  func searchItems(searchText: String) {
    self.dataResult.loading = true
    let url = "\(API.URL)\(searchText)"
    AF.request(url,method: .get, encoding: JSONEncoding(),headers: DataService.getHeader())
      .publishData()
      .sink { _ in }receiveValue: { (response) in
        self.dataResult.loading = false
        self.dataResult.getResult(response.response, data: response.data)
        if self.dataResult.result == nil || self.dataResult.result != nil && self.dataResult.result?.count == 0{
          self.dataResult.setError(errorObj: ErrorMessage(ErrorType.Empty.rawValue))
        }
      }
      .store(in: &subscription)
  }
}
4

1 回答 1

0

当使用@ObservedObject你的视图时,它被丢弃或重新渲染时被重新创建。所以你需要使用@StateObjectinSearchView而不是@ObservedObject.

你也不应该@ObservedObjectSimpleSearchModelView. 它应该在你的View.

@ObservedObject var monitor = NetworkMonitor()
于 2021-04-12T09:37:03.863 回答