I have this JSON-File coming from a MySQL-Database via PHP:
{
"id":1,
"partner":{
"id":1,
"name":"Migros Bank",
"image":"migrosbank"
},
"name":"Testkonto 1",
"type":"bank",
"iban":"CH12 1234 1234 1234 1234 1",
"datapoints":[
{
"id":1,
"depot_id":1,
"date":"2021-12-28",
"amount":5811.490234375
},
{
"id":2,
"depot_id":1,
"date":"2021-12-29",
"amount":7736.89013671875
}
]
}
In SwiftUI I decode it to a custom struct called Depot which consists of one instance of the custom struct Partner and an array of Instances of the custom struct Depotstand:
struct Depot: Hashable, Codable, Identifiable {
var id: Int
var partner: Partner? = nil
var name: String
var type: String
var iban: String? = nil
var datapoints: [Depotstand]? = nil
}
struct Partner: Hashable, Codable, Identifiable {
var id: Int
var name: String
var image: String
var imageName: Image {
Image(image)
}
}
struct Depotstand: Hashable, Codable, Identifiable {
var id: Int
var depot_id: Int
var date: Date
var amount: Double
}
I have a custom class called DataService which handles all the REST-stuff etc. which is referenced in every file/view that uses data from the REST-interface as @StateObject as in:
@StateObject private var DATA = DataService()
The structure is like this:
ContentView.swift (does not include DATA) loads Home.swift
Home.swift (includes DATA) loads DATA on the NavigationView wrapper async via .task (which works great) as in:
NavigationView {
// some stuff here
}
.task {
do {
try await DATA.readDepotAll()
try await DATA.readDepotstandAll()
} catch {
print(error.localizedDescription)
}
}
Home.swift loads DepotList.swift which shows a list of all Depots of a certain category (a click on a list item opens the view DepotDetails.swift which shows a list of all Depotstands aka datapoints)
In DepotDetails.swift is a + button in the toolbar which activates a sheet to insert a new Depotstand aka datapoint via DepotstandAdd.swift (which also works perfectly). But here is where my actual problem starts; the new datapoint can be inserted and when I print out the most current datapoint's value, the correct amount is displayed. But in the list of datapoints, the new added one is not reflected until I go one page back (DepotList.swift) which makes a refresh and shows the new total amount and then again on DepotDetails.swift which then also shows the refreshed data. I would like the data of DepotDetails.swift to be refreshed on completion of the sheet-action.
I attach the following code, DepotList.swift, DepotDetails.swift and DepotstandAdd.swift, as well as the relevant parts of the DataService which receives and posts the data over REST, since the post is already getting too long - sorry.
DepotList.swift
DepotDetails.swift
DepotstandAdd.swift
DataService.swift
Thanks for helping me out!
P.s: To be very clear; there is no error or failure message or something like this - everything just works as expected. I just need to know, how I can get the freshly inserted data to be displayed without having to manually reentering the view to let it doing it itself.
Post
Replies
Boosts
Views
Activity
My Preview Canvas does not work anymore.
I already uninstalled and freshly reinstalled Xcode (App Store Version and direct download from developer.apple.com, version 13.2 with Big Sur and now updated to Monterey 12.1). I uninstalled Xcode using AppCleaner and also by clearing the clang/cache folders, as well as Lib/Dev... etc. manually, but the same error comes up over and over again.
When I run the code directly (via Run/Simulator instead of Preview/Canvas) the app shows up and works just fine.
I open Xcode, I create a new project for iOS, I resume the preview and it fails with the attached error...
Preview Canvas Diagnostics
Any help would be greatly appreciated as at the moment, I don't have a running development environment unfortunately...
Thanks in advance.
Best regards
Silvio
I have this JSON-File coming from a MySQL-Database via PHP:
[
{
"id":1,
"partner":{
"id":1,
"name":"Migros Bank",
"image":"migrosbank"
},
"name":"Testkonto 1",
"type":"bank",
"iban":"CH12 1234 1234 1234 1234 1",
"datapoints":[
{
"id":1,
"depot_id":1,
"date":"2021-12-28",
"amount":5811.490234375
},
{
"id":2,
"depot_id":1,
"date":"2021-12-29",
"amount":7736.89013671875
}
]
},
{
"id":2,
"partner":{
"id":1,
"name":"Migros Bank",
"image":"migrosbank"
},
"name":"Testkonto 2",
"type":"bank",
"iban":"CH12 1234 1234 1234 1234 2",
"datapoints":[
{
"id":3,
"depot_id":2,
"date":"2021-12-28",
"amount":500
},
{
"id":4,
"depot_id":2,
"date":"2021-12-29",
"amount":1500
}
]
}
]
In SwiftUI I try to decode it to a custom struct called Depot which consists of one instance of the custom struct Partner and an array of Instances of the custom struct Depotstand:
import Foundation
import SwiftUI
struct Partner: Hashable, Codable, Identifiable {
var id: Int
var name: String
var image: String
var imageName: Image {
Image(image)
}
}
struct Depotstand: Hashable, Codable, Identifiable {
var id: Int
var depot_id: Int
var date: Date
var amount: Double
}
struct Depot: Hashable, Codable, Identifiable {
var id: Int
var partner: Partner
var name: String
var type: String
var iban: String
var datapoints: [Depotstand]
}
I've added a data-model, to get the JSON-data from my webserver - this part works fine - and then I try to decode the data to the custom struct Depot, which fails (it works, if I simplify the JSON/struct to only the simple Depot struct without depending on Partner and Depotstand instances):
import Foundation
final class ModelDataDepot: ObservableObject {
@Published var depots = [Depot]()
init(){
let url = URL(string: "https://api.webcoders.ch/index.php")!
URLSession.shared.dataTask(with: url) { (data, response, error) in
do {
if let data = data {
let decodedData = try JSONDecoder().decode([Depot].self, from: data)
DispatchQueue.main.async {
self.depots = decodedData
}
} else {
print("No Data!")
}
} catch {
print("JSON-Error: \(error)")
}
}.resume()
}
}
Here is the error I get which contains mainly data-type errors, but I have no idea to get around this:
JSON-Error: typeMismatch(Swift.Double, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "datapoints", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "date", intValue: nil)], debugDescription: "Expected to decode Double but found a string/data instead.", underlyingError: nil))
I've already tried some different data-casting, even using Numberformatters etc. but I can't get it to work...
Thanks for helping me out!
P.s: I am an absolute beginner to SwiftUI and mobile development in general... My coding background is mainly PHP, PowerShell and such, so please be patient with me. :-)