CoreData: why not refresh UI when book.isPublic toggled - core-data

'#Environment(.managedObjectContext) var context' will monitor the changes of context. When I click the button to toggle isPublic, I think it will cause to UI refreshing, Button text will change, But not, WHY?
struct BookInfoView: View {
#Environment(\.managedObjectContext) var context
var isPublic: Bool { return book.isPublic }
let book: Book
var body: some View {
print("refresh"); return
Group {
Button(action: onPublicToggled) {
Text(isPublic ? "private" : "public")
}
}
}
func onPublicToggled() {
context.perform {
book.isPublic.toggle()
try? context.save()
}
}
}

You need to observe a Book changes via ObservedObject wrapper, like
struct BookInfoView: View {
#Environment(\.managedObjectContext) var context
#ObservedObject var book: Book
var body: some View {
print("refresh"); return
Group {
Button(action: onPublicToggled) {
Text(book.isPublic ? "private" : "public")
}
}
}
func onPublicToggled() {
context.perform {
book.isPublic.toggle()
try? context.save()
}
}
}
Note: #Environment(\.managedObjectContext) var context relates to context itself, not to your CoreData objects.

Related

Fetch request must have an entity using #FetchRequest in SwiftUI

I am trying to use #FetchRequest to fetch all records from the SQLite database through CoreData. I have setup CoreData stack as shown below:
class CoreDataManager {
private let persistentContainer: NSPersistentContainer
static let shared = CoreDataManager()
var context: NSManagedObjectContext {
return persistentContainer.viewContext
}
private init() {
persistentContainer = NSPersistentContainer(name: "FooModel")
persistentContainer.loadPersistentStores { description, error in
if let error = error {
print("Unable to initialize Core Data \(error)")
}
}
}
}
#main
struct FooAppApp: App {
var body: some Scene {
WindowGroup {
ContentView().environment(\.managedObjectContext, CoreDataManager.shared.context)
}
}
}
When I try to access it is my ContentView as shown below:
struct ContentView: View {
#FetchRequest(entity: MyList.entity(), sortDescriptors: []) var myLists: FetchedResults<MyList>
var body: some View {
Text("Hello, world!")
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I get the following error:
A fetch request must have an entity.'
terminating with uncaught exception of type NSException
Am I missing something?
UPDATE: I removed the MyList.entity() part from the FetchRequest and now it works.
Remove
entity: MyList.entity(),
From the FetchRequest

swiftUI core data model

I have created a swiftUI view and trying to display a core data entity but the preview fails and the app crashes during runtime. How to instantiate a Movie object and use it as a state member in swiftUI?
import SwiftUI
struct SwiftUIView: View {
#Environment(\.managedObjectContext) private var viewContext
#State private var movie: Movie = Movie()
var body: some View {
Text(movie.title ?? "empty")
}
}
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
let moc = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
SwiftUIView().environment(\.managedObjectContext, moc)
}
}
You should create CoreData object with context, so here is possible way
struct SwiftUIView: View {
#Environment(\.managedObjectContext) private var viewContext
#State private var movie: Movie?
var body: some View {
Text(movie.title ?? "empty")
.onAppear {
self.movie = Movie(context: viewContext) // << here !!
}
}
}

SwiftUI Core Data Delete related Data of entity

im struggling to delete a related entity Data in SwiftUI.
I got 2 entities related.
Bean (main entity)
Shot (related entity)
many to one --> For each bean will be a lot of shots
When I try to build and run I get the error that bean is unresolved.
Here is my code:
import SwiftUI
struct ShotList: View {
#Environment(\.managedObjectContext) var moc
#FetchRequest(entity: Bean.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Bean.name, ascending: true)]) var beans: FetchedResults<Bean>
static let taskDateFormat: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .long
return formatter
}()
var body: some View {
NavigationView {
List {
ForEach(beans, id: \.self) { bean in
Section(header: HStack {
Text(bean.wrappedName)
Text(bean.wrappedRoaster)}) {
ForEach(bean.shotArray, id: \.self) { shot in
HStack {
Text(shot.wrappedTaste)
Text(shot.wrappedTexture)
Text("\(shot.yield, specifier: "%.0f") g")
Text("\(shot.doseAmount, specifier: "%.0f") g")
Text("\(shot.bruRatio, specifier: "%.0f")")
Text("\(shot.time, specifier: "%.0f") s")
Spacer()
Text("\(shot.dateOfShot ?? Date(), formatter: ShotList.taskDateFormat)").font(.footnote)
}
}.onDelete(perform: self.deleteShot)
.font(.body)
}
}
}.navigationBarTitle("Espresso Shotlist")
}
}
func deleteShot(at offset: IndexSet) {
for offset in offset {
let shot = bean.shotArray[offset]
moc.delete(shot)
}
try? moc.save()
}
}
struct ShotList_Previews: PreviewProvider {
static var previews: some View {
ShotList()
}
}
bean is only accessible in the closure: ForEach(beans, id: .self) { bean in
It won't be available in the func deleteShot.

How can I change a data value with CoreData in SwiftUI?

Change the value of a data using CoreData and SwiftUI
I'm trying to modify a boolean value isFavorite of a data with a button (heart) on a cell in a list.
Modele data
import Foundation
import CoreData
public class VoitureItem: NSManagedObject, Identifiable {
#NSManaged public var marque: String?
#NSManaged public var modele: String?
#NSManaged public var annee: String?
#NSManaged public var energie: String?
#NSManaged public var kilometrage: String?
#NSManaged public var isFavorite: Bool
}
extension VoitureItem {
static func getAllVoitureItems() -> NSFetchRequest<VoitureItem> {
let request:NSFetchRequest<VoitureItem> = VoitureItem.fetchRequest() as! NSFetchRequest<VoitureItem>
let sortDescriptor = NSSortDescriptor(key: "isFavorite", ascending: false)
request.sortDescriptors = [sortDescriptor]
return request
}
}
I use in my ContentView a managedObjectCOntext and a FetchRequest to show all the data in a List and to create new data.
I would like to know how I can just click on the button change the value of isFavorite in my CoreData.
ContentView() (main view)
struct ContentView: View {
#Environment(\.managedObjectContext) var managedObjectContext
#FetchRequest(fetchRequest: VoitureItem.getAllVoitureItems()) var voitureItems: FetchedResults<VoitureItem>
var body: some View {
NavigationView {
List {//show list of all the data}
}
}
Button on the cell
Button(action: {
//var fav: Bool = false
print("favorite")
//HERE - Change isFavorite in coreData
}) {
if isFavorite {
Image(systemName: "heart.fill")
.foregroundColor(Color.yellow)
} else {
Image(systemName: "heart")
.foregroundColor(Color.gray)
}
}
Something like this should work.
List(voitureItems) { item in
ItemCell(item: $item)
}
...
struct ItemCell: View {
#Binding var item: VoitureItem
var body: some View {
Button(action: {
item.isFavourite.toggle()
}) {
if item.isFavourite {
Image(systemName: "heart.fill")
.foregroundColor(Color.yellow)
} else {
Image(systemName: "heart")
.foregroundColor(Color.gray)
}
}
}
}

How to access goBack and goForward via UIViewRepresentable

I have added a WKwebview in SwiftUI via UIVewRepresentable. I am having difficulties getting some buttons to make the web view go back and go forward. Below is the two classes I have made but I am not even getting print to work ( maybe a bug? ).
import SwiftUI
import WebKit
struct Webview : UIViewRepresentable {
let request: URLRequest
func makeUIView(context: Context) -> WKWebView {
return WKWebView()
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.load(request)
}
func goBack(){
// go back
print("go back")
}
func goForward (){
// go forward
print("go forward")
}
}
import SwiftUI
struct FullWebView : View {
let webview = Webview(request: URLRequest(url: URL(string: "https://www.apple.com")!))
var body: some View {
VStack{
HStack{
Button(action: {
//do something
self.webview.goBack()
}){
Text("back")
}
Spacer()
Button(action: {
//do something
self.webview.goForward()
}){
Text("forward")
}
}
webview
}
}
}
dfd thank you for pointing me on the correct path, your answer needed a little modification because adding the webview as a var to the struct means I needed to set a value on it when I use it, so I also had to add a custom init method. Anyway here is the code that works.
Webview struct
import SwiftUI
import WebKit
struct Webview : UIViewRepresentable {
let request: URLRequest
var webview: WKWebView?
init(web: WKWebView?, req: URLRequest) {
self.webview = WKWebView()
self.request = req
}
func makeUIView(context: Context) -> WKWebView {
return webview!
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.load(request)
}
func goBack(){
webview?.goBack()
}
func goForward(){
webview?.goForward()
}
}
and here is the struct using the webview and adding the buttons to the top
import SwiftUI
struct FullWebView : View {
let webview = Webview(web: nil, req: URLRequest(url: URL(string: "https://www.apple.com")!))
var body: some View {
VStack{
HStack{
Button(action: {
//do something
self.webview.goBack()
}){
Text("back")
}
Spacer()
Button(action: {
//do something
self.webview.goForward()
}){
Text("forward")
}
}
webview
}
}
}

Resources