I need some help with this question please.
I am using SQLite.swift to store data in DB, I would like to fetch data only once when the application starts as it's a heavy operation. I need to be able to update / delete items from the user fields array in memory and display it accordingly in the DataScene.
The App contains a Tabbar at the bottom with 2 Tabs, Home and Data. The DataScene contains a list of all fields in the array. When an item in the user fields array has been updated or deleted from a Child view. The list in the DataScene needs to reflect that update.
The userFields array is defined in the TabBarScene and fetches the data from SQL just once because its declared as a lazy var in the Account object.
The sample code below works fine except the problem I have is when I switch between tabs. I need to be able to maintain the state of the array even when I switch between tabs so it keeps displaying the correct data, at the moment when you switch between tabs it displays the original records of the array.
Is this a 2 way binding issue? or do I need to use NSCache? I hope it's a simple issue. I hope I have provided enough information to the issue, otherwise please let me know. Thank you.
class Account {
lazy var userFields: [UserField] = { // Lazy var, fetches once
UserField.fetchAll()
}()
}
extension TabBarScene {
class TabBarViewModel: ObservableObject {
@Published var userFields: [UserField]
init() {
account = sessionService.account
userFields = account.userFields // Instantiated once
}
}
}
extension DataScene {
class DataViewModel: ObservableObject {
@Binding var userFields: [UserField]
init(_ userFields: Binding<[UserField]>) {
_userFields = userFields
}
func fetchData() {
userFields = account.userFields
}
}
}
extension FieldForm {
class FieldViewModel: ObservableObject {
@Binding var userFields: [UserField]
init(muserFields: Binding<[UserField]>) {
self._userFields = userFields
}
func deleteTapped() {
guard let label else { return }
let record = userFields.filter { $0.id == account.id && $0.name == name }.first
record?.delete()
if let record {
userFields.remove(record)
}
}
}
}
extension HomeScene {
class HomeViewModel: ObservableObject {
init() {
// Display another View
}
}
}