NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

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)

    }

   

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

   

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

   

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,

   

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

   

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

   

Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.