skip to Main Content

I have this model where I have a list of boat, and a list of works (which are linked to a boat). All are linked to the user. My data is stored in Firestore by my repository.

struct boat: Codable, Identifiable {
    @DocumentID var id : String?
    var name: String
    var description: String
    @ServerTimestamp var createdtime: Timestamp?
    var userId: String?
}

struct Work: Identifiable, Codable {
    @DocumentID var id : String?
    var title: String
    var descriptionpb: String
    var urgent: Bool
    @ServerTimestamp var createdtime: Timestamp?
    var userId: String?
    var boatId: String // (boatId = id of struct boat just above)
}

I have a view in which I want to display (and let the user edit) the details of the work (such as the title and descriptionpb), I manage to display the boatId (see below), however I want to display the boat name. How should I go about it?

import SwiftUI

struct WorkDetails: View {
    @ObservedObject var wcvm: WorkCellVM
    @ObservedObject var wlvm = WorklistVM()
    @State var presentaddwork = false
    var onCommit: (work) -> (Void) = { _ in }
    
    var body: some View {
        ScrollView {
            VStack(alignment: .leading) {
                Text(wcvm.work.boatId) // <-- THIS IS WHAT I WANT TO CHANGE INTO boat name instead of boatId
                    .padding()
                TextField("Enter work title", text: $wcvm.work.title, onCommit: {
                    self.onCommit(self.wcvm.work)
                })
                    .font(.title)
                    .padding()
                
                
                HStack {
                    TextField("Enter problem description", text: $wcvm.work.descriptionpb, onCommit: {
                        self.onCommit(self.wcvm.work)
                    })
                }
                .font(.subheadline)
                .foregroundColor(.secondary)
                .padding()
            }
        }
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    Solution found: when creating a work order, I was assigning the boat id, I am now assigning the boat name as well (and calling it in the work order display). Essentially keeping the same code as above, tweaking it a little bit so that it does what I want to do.


  2. Essentially you have a Data Model problem, not a SwiftUI problem. I would be keeping all of this in Core Data and linking the various models(Entities in Core Data) with relationships. So your Work(Essentially a work order) would link to the boat that the work was being performed on.

    Otherwise, you need to add a Boat as a parameter to Work. Either way would give you the desired syntax, but Core Data is much more efficient. It is also your data persistence model so you would kill two birds with one stone.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search