forked from UlricusR/iOS-EasyFPU
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComposedFoodItem+CoreDataClass.swift
More file actions
120 lines (96 loc) · 4.57 KB
/
ComposedFoodItem+CoreDataClass.swift
File metadata and controls
120 lines (96 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// ComposedFoodItem+CoreDataClass.swift
// EasyFPU
//
// Created by Ulrich Rüth on 05.11.20.
// Copyright © 2020 Ulrich Rüth. All rights reserved.
//
//
import Foundation
import CoreData
public class ComposedFoodItem: NSManagedObject {
static func fetchAll(viewContext: NSManagedObjectContext = AppDelegate.viewContext) -> [ComposedFoodItem] {
let request: NSFetchRequest<ComposedFoodItem> = ComposedFoodItem.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
guard let composedFoodItems = try? AppDelegate.viewContext.fetch(request) else {
return []
}
return composedFoodItems
}
static func deleteAll(viewContext: NSManagedObjectContext = AppDelegate.viewContext) {
ComposedFoodItem.fetchAll(viewContext: viewContext).forEach({
viewContext.delete($0)
})
try? viewContext.save()
}
static func create(from composedFoodItemVM: ComposedFoodItemViewModel) -> ComposedFoodItem {
debugPrint(AppDelegate.persistentContainer.persistentStoreDescriptions) // The location of the .sqlite file
let moc = AppDelegate.viewContext
var existingCDComposedFoodItem: ComposedFoodItem? = nil
// Check for existing ComposedFoodItem to be replaced
if let idToBeReplaced = composedFoodItemVM.cdComposedFoodItem?.id?.uuidString {
let predicate = NSPredicate(format: "id = %@", idToBeReplaced)
let request: NSFetchRequest<ComposedFoodItem> = ComposedFoodItem.fetchRequest()
request.predicate = predicate
if let result = try? moc.fetch(request) {
if !result.isEmpty {
existingCDComposedFoodItem = result[0]
}
}
}
// Remove ComposedFoodItem from CoreData if existing
// This will also delete the associated FoodItem, as delete rule was set to cascade in data model
if existingCDComposedFoodItem != nil {
moc.delete(existingCDComposedFoodItem!)
}
// Create new FoodItem
let cdComposedFoodItem = ComposedFoodItem(context: moc)
cdComposedFoodItem.id = UUID()
// Fill data
cdComposedFoodItem.name = composedFoodItemVM.name
cdComposedFoodItem.category = composedFoodItemVM.category.rawValue
cdComposedFoodItem.amount = Int64(composedFoodItemVM.amount)
cdComposedFoodItem.favorite = composedFoodItemVM.favorite
cdComposedFoodItem.numberOfPortions = Int16(composedFoodItemVM.numberOfPortions)
// Save before adding Ingredients, otherwise this could lead to an NSInvalidArgumentException
try? moc.save()
// Add new ingredients
for ingredient in composedFoodItemVM.foodItems {
let cdIngredient = Ingredient.create(from: ingredient)
cdComposedFoodItem.addToIngredients(cdIngredient)
}
// Save new composed food item
try? moc.save()
return cdComposedFoodItem
}
static func create(from existingComposedFoodItem: ComposedFoodItem) -> ComposedFoodItem {
let moc = AppDelegate.viewContext
let cdComposedFoodItem = ComposedFoodItem(context: moc)
cdComposedFoodItem.id = UUID()
// Fill data
cdComposedFoodItem.name = existingComposedFoodItem.name
cdComposedFoodItem.category = existingComposedFoodItem.category
cdComposedFoodItem.amount = existingComposedFoodItem.amount
cdComposedFoodItem.favorite = existingComposedFoodItem.favorite
cdComposedFoodItem.numberOfPortions = existingComposedFoodItem.numberOfPortions
// Add ingredients
if let existingIngredients = existingComposedFoodItem.ingredients?.allObjects as? [Ingredient] {
for ingredient in existingIngredients {
let cdIngredient = Ingredient.create(from: ingredient)
cdComposedFoodItem.addToIngredients(cdIngredient)
}
}
// Save new composed food item
try? moc.save()
return cdComposedFoodItem
}
static func delete(_ composedFoodItem: ComposedFoodItem) {
let moc = AppDelegate.viewContext
// Deletion of all related ingredients will happen automatically
// as we have set Delete Rule to Cascade in data model
// Delete the food item itself
moc.delete(composedFoodItem)
// And save the context
try? moc.save()
}
}