Home > Net >  I am displaying data built from a form to core data, then passing the data to another view but its n
I am displaying data built from a form to core data, then passing the data to another view but its n

Time:12-01

I have a form that will save info to core data, I then fetch that data and pass it to a product card that will be put into a list of product cards with different data.. when I click into the product card to go into the details of the card, the data is incorrect when viewed. Instead of it saying name2 it says name1 I would need the product detail to include the data that was displayed on the product card from the home view. I am trying to achieve this with Core Data.

Core Data will have title and subTitle as Strings

name, title, subTitle will be logged into the @NSManaged public var I'm using

Product cards details by a form from another view

// this struct will be on the home screen

struct productCard: View {

    @Environment (\.managedObjectContext) var managedObjContext
    @FetchRequest(entity: DataSet.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \DataSet.name, ascending: true)])

    var dataSet: FetchedResults <DataSet>
    var body: some View {
        ScrollView {
            HStack {
                ForEach( self.dataSet, id: \.self){ DataSet in
                    NavigationLink {
                        productCardDetails(dataSet: _dataSet)
                    } label: {  
                        HStack{
                            Text(DataSet.self.title ?? "Error")
                                .font(.title3)
                                .fontWeight(.bold)
                                .foregroundColor(.red)
                            Spacer()
                            Text(DataSet.self.subTitle ?? "Error")
                                .font(.caption)
                                .foregroundColor(.red)
                        }.padding(.horizontal, 10)
                    }
                }
            }
        }
    }
}

The product card will take the user into the productCardDetails

struct productCardDetails: View {
  @Environment (\.managedObjectContext) var managedObjContext
  @FetchRequest(sortDescriptors: []) var dataSet: FetchedResults <DataSet>

    var body: some View {
        ZStack(alignment: .bottom) {
            NavigationView {
                Form{
                    List(dataSet[0..<1], id:\.self) { DataSet in
                        Section{ 
                            Text(DataSet.self.title ?? "error")
                                .font(.title2)
                                .fontWeight(.bold)
                            Text(DataSet.self.subTitle ?? "error")
                                .font(.subheadline)
                                .fontWeight(.light)
                                .multilineTextAlignment(.leading)
                        }
                    }
                }
            }
        }
    }
}

For an example to demonstrates what happens is, when I have created 5 cards from the form I have, on the home screen it will show 5 productCard's, so it will be on the home view as productCard1, productCard2, productCard3, productCard4, productCard5 and in those cards they will all have their unique titles and subTitles. But for example when I click onto productCard4 it will display productCard1

CodePudding user response:

Well, you are using a collection in your productCardDetails View and in it only using the first entry. Better pass on that DataSet you want to show.

struct ProductCard: View {

    @Environment (\.managedObjectContext) var managedObjContext
    
    @FetchRequest(entity: DataSet.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \DataSet.name, ascending: true)])
    var dataSets: FetchedResults <DataSet>

    var body: some View {
            ScrollView{
                HStack{
                    ForEach( self.dataSets, id: \.self){ dataSet in
                        NavigationLink {
                            ProductCardDetails(dataSet: dataSet)
                        } label: {  HStack{
                                                Text(dataSet.title ?? "Error")
                                                    .font(.title3)
                                                    .fontWeight(.bold)
                                                    .foregroundColor(.red)
                                                Spacer()
                                                Text(dataSet.subTitle ?? "Error")
                                                    .font(.caption)
                                                    .foregroundColor(.red)
                                            }.padding(.horizontal, 10)}}}}}}}

and in the detail view:

struct ProductCardDetails: View {
      @Environment (\.managedObjectContext) var managedObjContext
      
      var dataSet: DataSet
        
        var body: some View {
            ZStack(alignment: .bottom){
                NavigationView {
                    Form{
                        Section{ Text(dataSet.title ?? "error")
                                    .font(.title2)
                                    .fontWeight(.bold)
                                Text(dataSet.subTitle ?? "error")
                                    .font(.subheadline)
                                    .fontWeight(.light)
                                    .multilineTextAlignment(.leading)}}}}}}

You really need to start using default naming conventions.

  • lowersize for variable names
  • uppersize for type names
  • pluralize collections
  • Related