Why is my view not passing condition from one scene but from other 2 scenes it work

51 Views Asked by At

I'm using this view to fetch detailed movie from API and then show it... The view works fine from all scenes but I'm using search state the view will not get through the if condition, but the print statements shows me optionals are not nil..

struct MovieDetailView: View {

@EnvironmentObject var state: GenreListState

@ObservedObject private var movieDetailState = MovieDetailState()

let movieId: Int

var body: some View {
    VStack {
        if movieDetailState.movie != nil && state.genres != nil {
            MovieDetailListView(movie: self.movieDetailState.movie!, genres: state.genres!)
        } 
        Text("Here")
            .onAppear() {
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                print(self.movieDetailState.movie ?? "no movie")
                print(self.state.genres ?? "no genres")
                }
            }
    }
    .navigationBarTitle(movieDetailState.movie?.title ?? "")
    .onAppear {
        self.movieDetailState.loadMovie(id: self.movieId)
        self.state.loadGenres()
    }
}

I'm accessing it through this view which is already filled with Movie array and therefore movieId is always valid and fetch is successful for detailed Movie

struct MoviePosterView: View {

let movies: [Movie]

var body: some View {
    ScrollView() {
        ForEach(self.movies) { movie in
            NavigationLink(destination: MovieDetailView(movieId: movie.id)) {
                MoviePosterCard(movie: movie)
            }.buttonStyle(PlainButtonStyle())
            .padding(EdgeInsets(top: 0, leading: 0, bottom: 10, trailing: 0))
        }
    }
    .padding(EdgeInsets(top: 10, leading: 20, bottom: 0, trailing: 20))
}

MovieListState

class MovieDetailState: ObservableObject {

private let movieService: NetworkingService
@Published var movie: Movie?
@Published var movies = [Movie]()
@Published var isLoading = false
@Published var error: NSError?

init(movieService: NetworkingService = NetworkingApi.shared) {
    self.movieService = movieService
}

func loadMovie(id: Int) {
    self.movie = nil
    self.isLoading = true
    self.movieService.fetchMovie(id: id) {[weak self] (result) in
        guard let self = self else { return }
        
        self.isLoading = false
        switch result {
        case .success(let movie):
            self.movie = movie
        case .failure(let error):
            self.error = error as NSError
        }
    }
}

func appendMovie(id: Int) {
    self.isLoading = false
    self.movieService.fetchMovie(id: id) {[weak self] (result) in
        guard let self = self else { return }
        
        self.isLoading = false
        switch result {
        case .success(let movie):
            self.movies.append(movie)
        case .failure(let error):
            self.error = error as NSError
        }
    }
}

GenreListState

class GenreListState: ObservableObject {

@Published var genres: [Genres]?
@Published var isLoading: Bool = false
@Published var error: NSError?

private let genreService: NetworkingService

init(genreService: NetworkingService = NetworkingApi.shared) {
    self.genreService = genreService
}

func loadGenres() {
    self.genres = nil
    self.isLoading = true
    self.genreService.getGenreList { [weak self] (result) in
        guard let self = self else { return }
        self.isLoading = false
        switch result {
        case .success(let response):
            self.genres = response.genres
        case .failure(let error):
            self.error = error as NSError
        }
    }
}
0

There are 0 best solutions below