What does the attribute `@EnvironmentObject` mean in the SwiftUI?
That's for example for Network purposes. You have a class conforming to BindableObject
(Beta 5: ObservableObject
) and send through the PassthroughSubject
(Beta 5: @Published
) your data.
For example this case:
SwiftUI (1.0)
struct Course: Decodable {
let name: String
}
class NetworkManager: ObservableObject {
@Published var courses = [Course]()
func getAllCourses() {
guard let url = URL(string: "https://api.letsbuildthatapp.com/jsondecodable/courses") else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
do {
let courses = try JSONDecoder().decode([Course].self, from: data!)
DispatchQueue.main.async {
self.courses = courses
}
} catch {
print("Failed To decode: ", error)
}
}.resume() // VERY IMPORTANT! As the request won't run
}
}
struct ContentView : View {
@ObservedObject var networkManager: NetworkManager = NetworkManager()
// Or this
// @EnvironmentObject var networkManager: NetworkManager // Don't forget to instaniate it with .environmentObject()
var body: some View {
VStack {
Button(action: {
self.networkManager.getAllCourses()
}, label: {
Text("Get All Courses")
})
List(networkManager.courses, id: \.name) {
Text($0.name)
}
}
}
}
Beta
struct Course: Decodable {
let name: String
}
class NetworkManager: BindableObject {
let didChange = PassthroughSubject<NetworkManager, Never>()
var courses = [Course]() {
didSet {
didChange.send(self)
}
}
func getAllCourses() {
guard let url = URL(string: "https://api.letsbuildthatapp.com/jsondecodable/courses") else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
do {
let courses = try JSONDecoder().decode([Course].self, from: data!)
DispatchQueue.main.async {
self.courses = courses
}
} catch {
print("Failed To decode: ", error)
}
}.resume() // VERY IMPORTANT! As the request won't run
}
}
struct ContentView : View {
@EnvironmentObject var networkManager: NetworkManager
var body: some View {
VStack {
Button(action: {
self.networkManager.getAllCourses()
}, label: {
Text("Get All Courses")
})
List(networkManager.courses.identified(by: \.name)) {
Text($0.name)
}
}
}
}