1

I have this two views:

import SwiftUI
import QGrid

struct CategoriasCanales: View {
    @Binding var logueado:Bool
    @ObservedObject var categorias = CargarCategoriasCanales()
    
    var body: some View {
        NavigationView{
            QGrid(self.categorias.listaCategoriasCanales, columns: 3){item in
                NavigationLink(destination:VerCanales(categoria: item.categoryID)){
                    Text(item.categoryName).frame(width:400, height:50)
                }
            }
            
        }
    }
}

and

import SwiftUI
import QGrid

struct VerCanales: View {
    
   
    private var categoria: String
    @ObservedObject private var canales: CargarCanales
  
    init(categoria: String) {
        print(categoria)
            self.categoria = categoria
            self.canales = CargarCanales(categoriaID: categoria)
        }
    
    var body: some View {
        NavigationView{
            QGrid(self.canales.listaCanales, columns: 3){item in
                NavigationLink(destination:PlayerVersa(canalID: item.streamID)){
                    Text(item.name).frame(width:400, height:50)
            
                }
          
            }
            
        }
    }
}

The problem is that when I enter the "CategoriasCanales" view, it runs the init of the "VerCanales" view a number of times equal to the number of categories.

I am new to SwiftUI, in DART for example, the Print() in the ChannelView init would only print the category that I am sending as a parameter, not all categories.

pawello2222
  • 46,897
  • 22
  • 145
  • 209

2 Answers2

1

Try using onAppear instead of init

    var body: some View {
        NavigationView{
            QGrid(self.canales.listaCanales, columns: 3){item in
                NavigationLink(destination:PlayerVersa(canalID: item.streamID)){
                    Text(item.name).frame(width:400, height:50)
            
                }
          
            }
            
        }
        .onAppear {
            print(categoria)
            self.categoria = categoria
            self.canales = CargarCanales(categoriaID: categoria)
        }
    }
AAV
  • 373
  • 1
  • 5
  • 17
  • Thanks for your help, I can't use the .onAppear() since I need to capture the variables before initializing to be able to pass them as argument of the ObservedObject – Fabian Reyes Nov 30 '20 at 23:28
1

The NavigationLink creates destination view in place of initialization, so you see called VerCanales.init as many as self.categorias.listaCategoriasCanales count.

I assume you wanted to defer creation of VerCanales: (used DeferView from https://stackoverflow.com/a/61242931/12299030)

NavigationLink(destination:DeferView { VerCanales(categoria: item.categoryID) }){
    Text(item.categoryName).frame(width:400, height:50)
}

Note: you don't need second NavigationView in VerCanales, only one navigation view should be in navigation stack and there is that one already in CategoriasCanales

Asperi
  • 228,894
  • 20
  • 464
  • 690