title: DispatchQueue triggering foreach loop twice with SwiftUI
body: <p>I'm having a weird behavior with my code.</p>
  <p>There is View with text that is getting its text values from the <code>User</code> object.</p>
  <p>If I take the reference out and leave it with just <code>String</code> everything is good, but with it the foreach loop that is getting this value runs twice.</p>
  <p>Also, if I put the assignment out from <code>DispatchQueue</code> it runs once but the view isn't updating itself (because its a <code>@Published</code> var).
  Even if I remove @Published it still run twice.</p>
  <p>I'm sure there is a simple explanation for this behavior but I really don't know what could make this happen.</p>
  <p>Better explanation in code with comments:</p>
  <p>The view:</p>
  <pre><code>struct MenuGroupBoxList: View {
      
      @EnvironmentObject var session: SessionStore
      
      var columns = [
          GridItem(.adaptive(minimum: 100, maximum: 200), spacing: 8),
          GridItem(.adaptive(minimum: 100, maximum: 200), spacing: 8)
      ]
      
      var body: some View {
          
          LazyVGrid(columns: columns, spacing: 8) {
              ForEach(MenuTitles.allCases, id:\.self) { item in
                  if item == .sentShifts{
                      GroupColumn(menuItem: item, unit: Text(session.user?.totalShiftConsts.description ?? &quot;0&quot;))
                  }else if item == .weekdays{
  
                      //Deleting this line: session.user?.totalShifts.description. will run the loop once.
  
                      GroupColumn(menuItem: item, unit: Text(session.user?.totalShifts.description ?? &quot;0&quot;))
                  }else{
                      GroupColumn(menuItem: item, unit: Text(&quot;0&quot;))
                  }
              }
          }.background(Color(UIColor.systemGroupedBackground))
      }
  }
  </code></pre>
  <p>Where the foreach loop:</p>
  <pre><code>enum ChooseWeekdays{
      case current, next
  }
  
  class WeekdaysModel: Identifiable, ObservableObject{
      
      private var session = SessionStore.shared
      private var date = Date()
  
      @Published var week = [Day]()
      @Published var isFetching = false
      
      init(_ weekdays: ChooseWeekdays){
          
          switch weekdays {
          case .current:
              currentWeekdaysFetch()
          case .next:
              nextWeekdaysFetch()
          }
          
      }
      
      private func currentWeekdaysFetch(){
  
          DispatchQueue.main.async {
              self.isFetching = true
              var count = 0
              for (index, symbol) in self.date.weekDaySymbols.enumerated(){
                  
                  let day = Day(date: self.date.currentWeek[index], isSelected: false, hasShifts: false, daySymbol: symbol, dayNumber: self.date.currentWeekNumbers[index])
                  
                  day.setShifts(morning: self.session.employeesList, middle: self.session.employeesList, evening: self.session.employeesList)
                  
                  if index == Date().getCurrentDayIndex(){
                      day.isSelected = true
                  }
                  
                  count = count + day.shifts.compactMap({$0.employees.contains(where: {$0.uid == self.session.user?.uid})}).count
  
                  
                  self.week.append(day)
                  self.isFetching = false
                  //Printing to see if loop run twice.
                  print(&quot;Index:&quot;, index)
              }
              //OR - take this line out from DispatchQueue, will run once but wouldn't update the UI because totalShifts is a @Published var
              self.session.user?.totalShifts = count
          }
          
      }
  }
  </code></pre>
  <p>The results I'm getting in print is <code>Index: 0...6</code> twice.</p>
  <p>Where <code>WeekdaysModel</code> created:</p>
  <pre><code>struct WeekdaysView: View {
      
      @ObservedObject var viewModel = WeekdaysModel(.current)
      
      var body: some View {
          ScrollView{
              WeekRow(model: viewModel)
              ForEach(viewModel.week){ day in
                  ShiftTypesView(day: day)
              }
          }
          .navigationTitle(LocalizedStringKey(&quot;Weekdays&quot;))
          .background(Color(UIColor.systemGroupedBackground))
          .edgesIgnoringSafeArea(.bottom)
      }
  }
  </code></pre>
  <p>There is another initiation in a different file but different enum (<code>WeekdaysModel(.next)</code>)</p>