Refresh value in text label from JSON

I am learning Swift to develop macOS applications and I ran into a problem. I am trying to get certain data from a JSON from the internet. I have managed to get such data and put it in simple text labels in the view but when I run Xcode and get the values, if the values from the JSON get updated, I can't see it reflected in my app. I know that I must perform a function to refresh the data but what I have always found is the function to refresh the data that is in a table, not a simple text label. So, basically to summarize, I get the values of the json file from url but if those values are updated, my app does not show them.

Values.swift
Code Block
import Foundation
struct WelcomeElement: Codable {
let link: String?
let state: String?
let editable: Bool?
let type, name: String?
let groupNames: [String]?
let label, category: String?
let members: [JSONAny]?
let groupType: String?
}
typealias Datos = [WelcomeElement]
class UserItems {
func loadData(completion: @escaping (Datos) -> Void) {
let url = URL(string: "http://192.168.0.15:8080/json")!
URLSession.shared.dataTask(with: url) { data, _, error in
if let error = error { print(error); return }
do {
let items = try JSONDecoder().decode([WelcomeElement].self, from: data!)
completion(items)
} catch {
print(error)
}
}.resume()
}
}


ViewController.swift
Code Block
import Cocoa
class ViewController: NSViewController, NSTextFieldDelegate {
let userItems = UserItems()
@IBOutlet var itemSalida: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
userItems.loadData { users in
print(users)
if let position = users.firstIndex(where: { $0.name == "test2" }) {
DispatchQueue.main.async {
print(users[position].state!)
self.itemSalida.stringValue = users[position].state ?? "na"
}
}
}
}
}

A sample of the json in the url:
Code Block
{
"link": "null",
"state": "NULL",
"editable": false,
"type": "String",
"name": "m",
"tags": [],
"groupNames": []
},
{
"link": "test1",
"state": "-1",
"stateDescription": {
"pattern": "%d",
"options": []
},
"editable": true,
"type": "Number",
"name": "test2",
"label": "Current",
"category": "",
"tags": [
"Point"
],
"groupNames": []
}
---

Any help will help me! Thanks!
Answered by Claude31 in 674341022
You request the JSON data, so that cannot be done automatically.

You have several options:
  • create a timer (frequency 10s ? 60 s ? depends on the data nature) to regularly call for loadData as you did in viewDidLoad and thus update labels

  • you could also tigger by user:

             with a refresh button on the view
             or when user taps on the table
  • have the server push you notifications and process when receiving.

For the first solution, don't forget to kill the timer when you leave the view
Accepted Answer
You request the JSON data, so that cannot be done automatically.

You have several options:
  • create a timer (frequency 10s ? 60 s ? depends on the data nature) to regularly call for loadData as you did in viewDidLoad and thus update labels

  • you could also tigger by user:

             with a refresh button on the view
             or when user taps on the table
  • have the server push you notifications and process when receiving.

For the first solution, don't forget to kill the timer when you leave the view

I can't see it reflected in my app.

What do you see instead?

As far as I tried your code, the NSTextField itemSalida showed -1, which seems to be the expected value.
(I needed to simulate URLSession as I cannot access 192.168.0.15:8080, to fill JSONAny and to correct the JSON text by adding [ and ].)

I know that I must perform a function to refresh the data

What do you think you must? Your code is enough to update the content of the label.

if those values are updated, my app does not show them

When or where are those values updated? Other than your loadData?
Refresh value in text label from JSON
 
 
Q