Your post has been very helpful to me. Thank you.
I would like to ask you if you can create a swift file that groups all the methods (addTask, updateTask, ect) that can then be used in each view.
In swift with uikit I had create CoredataHelper shared class for use in every app with little modify.
Thank you
You definitely can! I do this in my production apps. I generally then add the helper as an EnvironmentObject so that I can access it when I need it.
You can see my messy source code for one of my apps here. The HelperClasses folder will have the CoreData helper CalculationManager.swift. This updates and deletes items from my CoreData context.
Great solution. But, wanting to move from ContentView to CoreDataHelper:
@Environment(.managedObjectContext) var context
and
@FetchRequest(
entity: Task.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \Task.dateAdded, ascending: false)],
predicate: NSPredicate(format: "isComplete == %@", NSNumber(value: false))
) var notCompletedTasks: FetchedResults
How can it be done?
It's possible?
Thank you.
You can, but the @Environment and @FetchRequest are used with SwiftUI views. I wouldn't move the @Environment to make sure that there is a context associated with the view. I also like to leave my FetchRequests in the view because that's where I need the data to be viewed.
Your post has been very helpful to me. Thank you.
I would like to ask you if you can create a swift file that groups all the methods (addTask, updateTask, ect) that can then be used in each view.
In swift with uikit I had create CoredataHelper shared class for use in every app with little modify.
Thank you
You definitely can! I do this in my production apps. I generally then add the helper as an
EnvironmentObject
so that I can access it when I need it.You can see my messy source code for one of my apps here. The
HelperClasses
folder will have the CoreData helperCalculationManager.swift
. This updates and deletes items from my CoreData context.Thank you. It is very difficult, for me. Could you tell me how to extract the methods from your TaskList, since I don't think I can do it?
Here's a branch of the original project with what you're looking to do that's been implemented.
Create a new file with the functions like this one linked here.
In the
ContentView.struct
file, addlet core_data_helper = CoreDataHelper()
. Then in the button's actions, call the functions fromcore_data_helper
.Here's an example of
addTask()
:Here's a link to what
ContentView.swift
should look like.Great solution. But, wanting to move from ContentView to CoreDataHelper:
@Environment(.managedObjectContext) var context
and
@FetchRequest(
entity: Task.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \Task.dateAdded, ascending: false)],
predicate: NSPredicate(format: "isComplete == %@", NSNumber(value: false))
) var notCompletedTasks: FetchedResults
How can it be done?
It's possible?
Thank you.
You can, but the
@Environment
and@FetchRequest
are used with SwiftUI views. I wouldn't move the@Environment
to make sure that there is a context associated with the view. I also like to leave my FetchRequests in the view because that's where I need the data to be viewed.Your explanation convinced me. Thank you.