capitulo 5:

PHOTO EMBED

Mon Feb 27 2023 00:59:39 GMT+0000 (Coordinated Universal Time)

Saved by @swiftui_spanish #swift #swiftui

// Capítulo 5: Actualización y eliminación de datos en CoreData

// Para actualizar y eliminar objetos en CoreData utilizando SwiftUI, podemos utilizar el contexto de CoreData. A continuación se muestra:

Button("Update") {
    let request: NSFetchRequest<Product> = Product.fetchRequest()
    request.predicate = NSPredicate(format: "name == %@", "iPhone")
    
    do {
        let products = try context.fetch(request)
        if let product = products.first {
            product.price = 899.99
            try context.save()
        }
    } catch {
        print(error.localizedDescription)
    }
}

Button("Delete") {
    let request: NSFetchRequest<Product> = Product.fetchRequest()
    request.predicate = NSPredicate(format: "name == %@", "iPhone")
    
    do {
        let products = try context.fetch(request)
        if let product = products.first {
            context.delete(product)
            try context.save()
        }
    } catch {
        print(error.localizedDescription)
    }
}
content_copyCOPY