Hi, a Service class where I process all Core Data transactions for a single entity/Object and I often find my self needing to access information from other classes and I was wondering if there was an issue by calling these classes in non-UI related classes.
In the following code I'm calling the dogs
array from the DogService
class inside the DogHouseService
class, and I was wondering if this could be an issue.
Are there any possible issue by calling @Published
properties from an ObservableObject
class inside other classes?
ObservableObject class
class DogService: ObservableObject{
let manager: CoreDataManager
@Published var dogs: [Dog] = []
init(coreDataManager: CoreDataManager = .instance){
self.manager = coreDataManager
loadDogs()
}
//Adds, Deletes, Updates, etc.
func loadDogs(){
let request = NSFetchRequest<Dog>(entityName: "Dog")
do{
dogs = try manager.context.fetch(request)
}catch let error{
print("Error fetching dogs. \(error.localizedDescription)")
}
}
func save(){
self.manager.save()
}
}
Other Class
class DogHouseService{
let dogService = DogService()
for dog in dogService.dogs{
// do something
}
}