UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Core Data - How to refresh List View with wrapped values after successful save in detail view.

Forums > SwiftUI

Hi, Need some help on refreshing views with CoreData. I have a list of NSManagedObjects that show a card for each item. When selected it shows the detail view. I successfully save objects in the detail view and all of the CD items update correctly on both the detail view and list view. My problem is the card in the list view shows data based on the wrappedValues. That is not updating correctly. How do I refresh the display so that wrappedValues update?

Here is my code for the List View:

GenericFilteredListVGrid(predicate: predicate, sortDescriptors: [], count: $count) { (item: Opportunity) in

      Group {
          NavigationLink(destination:  OpportunityView(opportunity: item)) {
              LeadCard(lead: item)
          }
      }
  }
.onAppear(perform: updateUI)

func updateUI() {
    DispatchQueue.main.async {
        let temp = predicate
        predicate = temp
    }
}

Here is the code inside of LeadCard

      // NSManagedObject
   public let item: Opportunity

    public init(lead: Opportunity) {
       item = lead
    }

// in body
  HStack {
        Text(item.wrappedType)
            .fontWeight(.bold)

        Text((item.wrappedDaysSinceLastTouch != 0) ? ":  Touched \(item.wrappedDaysSinceLastTouch) \((item.wrappedDaysSinceLastTouch > 1) ? "days" : "day") ago." : "")
            .font(.footnote)

        Spacer()
        // Image from enum
        StatusType.returnImage(for: StatusType(rawValue: item.wrappedLatestStatus)!)
            .resizable()
            .frame(width: 30, height: 30)
            .foregroundColor(.orange)
            .padding(.trailing, 10)

    }

3      

3      

Hi, So i viewed this link and it did not help? I am able to update attributes from the model with no problem.

My challenge is that based on the updated new attribute I want to change an image presented. I am currently using a wrappedValue. The wrapped The List view does not update with the wrapped properties.

Any help would be appreciated?

Thanks

3      

you should try to make your item an @ObservedObject on your lead card to refresh that view in case the item changes.

3      

Hi, Thanks. I didn't think we could make NSManagedObjects an ObservedObject? I did find a working solution below where a changed the CardView to recieve a binding NSManagedObject and it works well.

    GenericFilteredListVGrid(predicate: predicate, sortDescriptors: [], count: $count) { (item: Opportunity) in

      Group {
          NavigationLink(destination:  OpportunityView(opportunity: item)) {
              LeadCard(lead: .constant(item))
          }
      }
  }

Inside LeadCard.

@Binding public var item: Opportunity

public init(lead: Binding<Opportunity>) {
    _item = lead
}

Thanks for your help,

3      

hi,

for the record, the NSManagedObject class conforms to the ObervableObject protocol out of the box. a change to any of its @NSManaged properties triggers an objectWillChange.send() message. if you have a Core Data object as a parameter in a View, say, you can certainly mark it as an @ObservedObject.

hope that helps,

DMG

3      

DMG, Yes. I did not realize that. I will give that a try as well. Thanks

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.