Modify @State value from a function in another file

Hi guys!
Sincerely I don't know what title should I put to this post. Anyway, hope you can help me :)
I have a function that request some data to my webserver. Everything works fine when the function is in the same structure as the body, but when I write this function in a different file, and then call it to my Struct (body file), the @State that should return is not returning it anymore ( it is empty).
Here is my code:

Function file
Code Block swift
import SwiftUI
struct DescriptionFunction {
// Result variable
@State var companyDescriptionResult = [CompanyDescriptionResult]()
// Define function API Request
func descriptionRequest(url: String) {
// First replace spaces for & in the parameter (searchText)
let urlEncode = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? url
guard let url = URL(string: urlEncode) else {
print("Invalid URL")
return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, reponse, error in
if let data = data {
if let decodedResponse = try? JSONDecoder().decode(CompanyDescriptionResponse.self, from: data) {
DispatchQueue.main.async {
self.companyDescriptionResult = decodedResponse.results
}
return
}
}
print("Fetch failed: \(error?.localizedDescription ?? "Unknown error")")
}.resume()
}
}

View
Code Block swift
struct CompanyDescriptionScreen: View {
var descriptionFunction = DescriptionFunction()
var body: some View {
VStack {
Text("1")
.onAppear {
descriptionFunction.descriptionRequest(url: "myURL")
}
Button(action: {print(descriptionFunction.companyDescriptionResult)}) {
Text("print")
}
}
}
}

When I click this button, it prints an empty array, when it shouldn't. If I write the function directly in the same view file and structure, it returns the actual value.

Thank you.
Dennis

Answered by OOPer in 637204022
Unfortunately, @State variables are managed by SwiftUI only when it is a property of a View.
If it is not a property of a View, it does not work and all the values set to the variables are silently ignored. (Even in the same file.)

In your case, using ObservableObject and @Published seems to be the right solution.

Function file
Code Block
class DescriptionFunction: ObservableObject { //<-
// Result variable
@Published var companyDescriptionResult = [CompanyDescriptionResult]() //<-
//...
}


View
Code Block
struct CompanyDescriptionScreen: View {
@ObservedObject var descriptionFunction = DescriptionFunction() //<-
//...
}


You may want to use @StateObject or @EnvironmentObject instead of @ObservedObject.
Accepted Answer
Unfortunately, @State variables are managed by SwiftUI only when it is a property of a View.
If it is not a property of a View, it does not work and all the values set to the variables are silently ignored. (Even in the same file.)

In your case, using ObservableObject and @Published seems to be the right solution.

Function file
Code Block
class DescriptionFunction: ObservableObject { //<-
// Result variable
@Published var companyDescriptionResult = [CompanyDescriptionResult]() //<-
//...
}


View
Code Block
struct CompanyDescriptionScreen: View {
@ObservedObject var descriptionFunction = DescriptionFunction() //<-
//...
}


You may want to use @StateObject or @EnvironmentObject instead of @ObservedObject.
Thank you so much OOPer, it works perfectly. I'm new at programming and I'm trying to learn it. Doing my best :D
Modify @State value from a function in another file
 
 
Q