HomeiOS Developmentios - Coredata entity is being up to date however view just...

ios – Coredata entity is being up to date however view just isn’t refreshing


I’ve two entities which have a one-to-many relationship between them, the place mainly a invoice can have many merchandise on it, as proven: Invoice Entity and Product Entity.

Their courses:

extension Invoice {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Invoice> {
        return NSFetchRequest<Invoice>(entityName: "Invoice")
    }

    @NSManaged public var id: UUID?
    @NSManaged public var title: String?
    @NSManaged public var date: Date?
    @NSManaged public var merchandise: NSSet?

    public var unwrappedID: UUID {
        id ?? UUID()
    }
    
    public var unwrappedName: String {
        title ?? "Unknown Invoice Title"
    }
    
    public var unwrappedDate: Date {
        date ?? Date.now
    }
    
    public var formattedDate: String {
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "pt_BR")
        formatter.timeZone = TimeZone(identifier: "GMT-3")
        formatter.dateFormat = "EEEE, MMM d, yyyy, HH:mm"
        return formatter.string(from: unwrappedDate)
    }
    
    public var productsArray: [Product] {
        let set = merchandise as? Set<Product> ?? []
        
        return set.sorted {
            $0.unwrappedName < $1.unwrappedName
        }
    }
    
}

// MARK: Generated accessors for has
extension Invoice {

    @objc(addHasObject:)
    @NSManaged public func addToProducts(_ worth: Product)

    @objc(removeHasObject:)
    @NSManaged public func removeFromProducts(_ worth: Product)

    @objc(addHas:)
    @NSManaged public func addToProducts(_ values: NSSet)

    @objc(removeHas:)
    @NSManaged public func removeFromProducts(_ values: NSSet)

}

extension Invoice : Identifiable {

}

and

extension Product {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Product> {
        return NSFetchRequest<Product>(entityName: "Product")
    }

    @NSManaged public var id: UUID?
    @NSManaged public var title: String?
    @NSManaged public var amount: Int32
    @NSManaged public var value: Double
    @NSManaged public var invoice: Invoice?
    
    public var unwrappedId: UUID {
        id ?? UUID()
    }
    
    public var unwrappedName: String {
        title ?? "Unknown product title"
    }

}

extension Product : Identifiable {

}

Once I need to create or delete an object the view (listing) refresh usually the next are the capabilities I am utilizing to take action

    personal func addBill () {
        withAnimation {
        
            let invoice = Invoice(context: managedObjectContext)
            invoice.id = UUID()
            invoice.title = billName
            invoice.date = Date.now
        
            strive? managedObjectContext.save()
            
            billName = ""
            hideKeyboard()
            isShowing = false
        }
    }

    func deleteBill (offsets: IndexSet) {
        for offset in offsets {
            let invoice = payments[offset]
            managedObjectContext.delete(invoice)
        }
        
        strive? managedObjectContext.save()
    }

    personal func addProduct () {
        withAnimation {
            let product = Product(context: managedObjectContext)
            product.id = UUID()
            product.title = productName
            product.value = Double(productPrice) ?? 0.0
            product.amount = 1
            product.invoice = selectedBill
            product.invoice?.id = selectedBill.id
            product.invoice?.date = selectedBill.date
            product.invoice?.title = selectedBill.unwrappedName
            
            strive? managedObjectContext.save()
            
            productName = ""
            hideKeyboard()
            isShowing = false
        }
    }

    personal func deleteProduct (offsets: IndexSet) {
        for offset in offsets {
            let product = selectedBill.productsArray[offset]
            managedObjectContext.delete(product)
        }
        
        strive? managedObjectContext.save()
    }

My situation occurs when I attempt to replace the amount of a product. I’ve an particular view referred to as Counter which has a plus button and a minus button and after I click on on them the core knowledge modifications however the view just isn’t refreshing, I can solely see the modifications within the view if I’m going again to the ancestors view and open the listing os merchandise once more.

That is my Counter class

struct CounterView: View {
    // MARK: - PROPERTIES
    @Atmosphere(.managedObjectContext) var managedObjectContext
    
    @State var selectedProduct: Product
    
    var physique: some View {
        HStack {
            Button(motion: {
                selectedProduct.amount += 1
                strive? self.managedObjectContext.save()
            }, label: {
                Picture(systemName: "plus")
                    .foregroundColor(comandinhaGold)
                    .body(width: 25, top: 25)
            })
            .buttonStyle(BorderlessButtonStyle())
            
            Textual content("(selectedProduct.amount)")
            
            Button(motion: {
                if selectedProduct.amount > 1 {
                    selectedProduct.amount -= 1
                }
                strive? self.managedObjectContext.save()
            }, label: {
                Picture(systemName: "minus")
                    .foregroundColor(comandinhaGold)
                    .body(width: 25, top: 25)
            })
            .buttonStyle(BorderlessButtonStyle())
        }
    }
}

i take advantage of fetchrequest to get the listing of payments and when a invoice is chosen that is how I listing the merchandise

   Record {
     ForEach(selectedBill.productsArray, id: .id) { product in
        HStack (spacing: 16){
           VStack(alignment: .main, spacing: 5) {
              Textual content(product.unwrappedName)
                .foregroundColor(comandinhaGold)
                .font(.headline)
              Textual content("Preço un.: (String(format: "R$ %.2f", product.value))")
                .font(.footnote)
           }
                                
           Spacer()
                                
           CounterView(selectedProduct: product)
        }
     }
     .onDelete(carry out: deleteProduct)
  }

I’ve tried forcing the view to refresh utilizing issues like

@State var refresher = false


func updateView {
    refresher.toggle()
}

and different options like that and nothing appears to work.

Tried to @Binding, @StateObject, nothing actually makes the textual content with amount updates with te new worth upon a click on on the button.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments