Skip to content

Commit 3c53890

Browse files
committed
refactor coredata to be singleton
1 parent c2ec483 commit 3c53890

3 files changed

Lines changed: 21 additions & 18 deletions

File tree

AnimalsApp/AnimalsApp/Services/CoreData.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import UIKit
99
import CoreData
1010

1111
protocol CoreDataContract: AnyObject {
12+
var favoriteAnimals: [FavoriteAnimal] { get set }
1213
func loadFavoriteAnimals(completion: @escaping () -> ())
1314
func isFavorite(id: String) -> Bool
1415
func addFavorite(_ animal: Animal)
@@ -17,16 +18,16 @@ protocol CoreDataContract: AnyObject {
1718
}
1819

1920
class CoreData: CoreDataContract {
20-
var managedContext: NSManagedObjectContext?
21-
static var favoriteAnimals = [FavoriteAnimal]() {
21+
static let shared = CoreData()
22+
23+
private var managedContext: NSManagedObjectContext?
24+
var favoriteAnimals = [FavoriteAnimal]() {
2225
didSet {
23-
notifyChanges?()
26+
//completar
2427
}
2528
}
2629

27-
static var notifyChanges: (() -> Void)?
28-
29-
init() {
30+
private init() {
3031
managedContext = (UIApplication.shared.delegate as? AppDelegate)?
3132
.persistentContainer
3233
.viewContext
@@ -38,7 +39,7 @@ class CoreData: CoreDataContract {
3839
let fetchRequest: NSFetchRequest<FavoriteAnimal> = FavoriteAnimal.fetchRequest()
3940

4041
do {
41-
CoreData.favoriteAnimals = try managedContext.fetch(fetchRequest)
42+
self.favoriteAnimals = try managedContext.fetch(fetchRequest)
4243
completion()
4344
} catch let error as NSError {
4445
print("Could not fetch. \(error), \(error.userInfo)")
@@ -47,7 +48,7 @@ class CoreData: CoreDataContract {
4748

4849
func isFavorite(id: String) -> Bool {
4950

50-
for animal in CoreData.favoriteAnimals {
51+
for animal in self.favoriteAnimals {
5152
if animal.id == id {
5253
return true
5354
}
@@ -66,7 +67,7 @@ class CoreData: CoreDataContract {
6667
newFavoriteAnimal.descript = animal.description
6768
newFavoriteAnimal.age = Int32(animal.age ?? 0)
6869
newFavoriteAnimal.species = animal.species
69-
CoreData.favoriteAnimals.append(newFavoriteAnimal)
70+
self.favoriteAnimals.append(newFavoriteAnimal)
7071
}
7172

7273
func removeFavorite(id: String) {
@@ -75,10 +76,10 @@ class CoreData: CoreDataContract {
7576

7677
var count = 0
7778

78-
for animal in CoreData.favoriteAnimals {
79+
for animal in self.favoriteAnimals {
7980
if animal.id == id {
8081
let removeAnimal = animal
81-
CoreData.favoriteAnimals.remove(at: count)
82+
self.favoriteAnimals.remove(at: count)
8283
managedContext.delete(removeAnimal)
8384
}
8485
count += 1

AnimalsApp/AnimalsApp/View Models/FavoritesViewModel.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ import Foundation
99

1010
class FavoritesViewModel {
1111
var favoriteAnimals = [Animal]()
12+
let coreData: CoreDataContract
13+
14+
init(coreData: CoreDataContract = CoreData.shared) {
15+
self.coreData = coreData
16+
}
1217

1318
func getFavoriteAnimals(completion: @escaping () -> Void) {
14-
let coreDataAnimals = CoreData.favoriteAnimals
15-
favoriteAnimals.removeAll(keepingCapacity: true)
16-
17-
coreDataAnimals.forEach { favoriteAnimal in
18-
let newAnimal = Animal(id: favoriteAnimal.id, name: favoriteAnimal.name, description: favoriteAnimal.descript, age: Int(favoriteAnimal.age), species: favoriteAnimal.species, isFavorite: true, imageData: favoriteAnimal.image)
19-
favoriteAnimals.append(newAnimal)
19+
20+
favoriteAnimals = coreData.favoriteAnimals.map { favoriteAnimal in
21+
Animal(id: favoriteAnimal.id, name: favoriteAnimal.name, description: favoriteAnimal.descript, age: Int(favoriteAnimal.age), species: favoriteAnimal.species, isFavorite: true, imageData: favoriteAnimal.image)
2022
}
2123

2224
completion()

AnimalsApp/AnimalsApp/View Models/HomeViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class HomeViewModel {
1212
private var coreData: CoreDataContract
1313
var animals = [Animal]()
1414

15-
init(webServices: WebServicesContract = WebServices(), coreData: CoreDataContract = CoreData()) {
15+
init(webServices: WebServicesContract = WebServices(), coreData: CoreDataContract = CoreData.shared) {
1616
self.webServices = webServices
1717
self.coreData = coreData
1818
}

0 commit comments

Comments
 (0)