TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Updating a Scroll View with a global array

Forums > SwiftUI

On my main screen I have a Scroll View which I want to display all of the events that are happening within the app. Currently I have this defined as a global variable as shown below:

struct matchEvent: Identifiable{
    var id = UUID()

    var time: String
    var event: String
    var player: String
}

var matchEvents = [matchEvent]()

func addEvent(time: String, event: String, player: String){
    matchEvents.append(matchEvent(time: time, event: event, player: player))
}

The function addEvent() is called from inside different methods of other classes around the app, hence why I made it global. How do I get the app to update the scroll view when the array matchEvents is updated? I have put my ScrollView code below.

struct eventList: View{
    var body: some View{
        ScrollView(.horizontal){
            HStack(matchEvents){
                ForEach(matchEvents){event in
                    Button(event.event){
                        print("Clicked")
                    }
                }
            }
        }
    }
}

Any help would be appreciated

3      

hi Ben,

i would encapsulate the matchEvents array and the addEvent method in an object (a class) that conforms to the ObservableObject protocol, say something like this

class EventManager: ObservableObject {
  @Published var matchEvents = [matchEvent]()

  func addEvent(time: String, event: String, player: String) {
    matchEvents.append(matchEvent(time: time, event: event, player: player))
  }
}

create a global instance of this class, perhaps var globalEventManager = EventManager(), and then in any View that you want to respond to changes in the array, create a local reference to this variable that is marked with @ObservedObject. so your eventList view would be this:

struct eventList: View{
    @ObservedObject var eventManager = globalEventManager
    var body: some View{
        ScrollView(.horizontal){
            HStack { // <<-- HStack alone here, without the ()?
                ForEach(eventManager.matchEvents){event in
                    Button(event.event){
                        print("Clicked")
                    }
                }
            }
        }
    }
}

i think that's OK. hope that helps,

DMG

4      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.