-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSV import with DataController working now
- Loading branch information
Showing
7 changed files
with
139 additions
and
42 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+7.24 KB
(110%)
...odeproj/project.xcworkspace/xcuserdata/D032610.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// | ||
// DataController.swift | ||
// CookingFam | ||
// | ||
// Created by Alexis on 24.05.23. | ||
// | ||
|
||
import Foundation | ||
import CoreData | ||
|
||
class DataController : ObservableObject{ | ||
let container = NSPersistentContainer(name: "Model") | ||
|
||
init(){ | ||
container.loadPersistentStores{ _, error in | ||
if let error = error{ | ||
print("CoreData Error:\(error.localizedDescription)") | ||
return | ||
} | ||
self.container.viewContext.mergePolicy = NSMergePolicy.mergeByPropertyStoreTrump | ||
} | ||
} | ||
|
||
func importCSV(){ | ||
//CSV structure: id,title,ingredients,directions,link,source,NER | ||
|
||
guard let path = Bundle.main.path(forResource: "Recipe", ofType: "csv") else { | ||
print("CSV not found") | ||
return | ||
} | ||
|
||
do{ | ||
let url = URL(fileURLWithPath: path) | ||
let data = try Data(contentsOf: url) | ||
let csvString = String(data: data, encoding: .utf8) | ||
let rows = csvString?.components(separatedBy: "\n") | ||
|
||
|
||
if let rows = rows { | ||
for row in rows { | ||
// seperade data from nested data , e.g. A,[X,Y,Z],B,C | ||
var nested = false | ||
var field = "" | ||
var data_columns: [String] = [] | ||
if row.contains(","){ | ||
for c in row { | ||
if c == "[" || c == "]" { | ||
nested.toggle() | ||
}else if c == "," && !nested { | ||
data_columns.append(field) | ||
field = "" | ||
}else{ | ||
field.append(c) | ||
} | ||
} | ||
if !field.isEmpty { | ||
data_columns.append(field) | ||
} | ||
} | ||
if data_columns != []{ | ||
if let newRecipe = NSEntityDescription.insertNewObject(forEntityName: "Recipe", into: container.viewContext) as? Recipe { | ||
newRecipe.name = data_columns[1] | ||
let ingredients = data_columns[2].components(separatedBy: ",") | ||
ingredients.forEach{ingredient in | ||
if let newIngredient = NSEntityDescription.insertNewObject(forEntityName: "Ingredient", into: container.viewContext) as? Ingredient { | ||
newIngredient.name = ingredient | ||
newIngredient.recipe = newRecipe | ||
newIngredient.recipe?.name = data_columns[1] | ||
} | ||
} | ||
let directions = data_columns[3].components(separatedBy: ",") | ||
directions.forEach{direction in | ||
if let newDirection = NSEntityDescription.insertNewObject(forEntityName: "Direction", into: container.viewContext) as? Direction { | ||
newDirection.text = direction | ||
newDirection.recipe = newRecipe | ||
newDirection.recipe?.name = data_columns[1] | ||
} | ||
} | ||
let components = data_columns[6].components(separatedBy: ",") | ||
components.forEach{component in | ||
if let newComponent = NSEntityDescription.insertNewObject(forEntityName: "Component", into: container.viewContext) as? Component { | ||
newComponent.name = component | ||
newComponent.recipe = newRecipe | ||
newComponent.recipe?.name = data_columns[1] | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
try container.viewContext.save() | ||
} catch { | ||
print("Error while importing CSV: \(error)") | ||
} | ||
} | ||
|
||
func deleteAll() { | ||
let fetchRequest1: NSFetchRequest<NSFetchRequestResult> = Recipe.fetchRequest() | ||
let batchDeleteRequest1 = NSBatchDeleteRequest(fetchRequest: fetchRequest1) | ||
_ = try? container.viewContext.execute(batchDeleteRequest1) | ||
|
||
let fetchRequest2: NSFetchRequest<NSFetchRequestResult> = Ingredient.fetchRequest() | ||
let batchDeleteRequest2 = NSBatchDeleteRequest(fetchRequest: fetchRequest2) | ||
_ = try? container.viewContext.execute(batchDeleteRequest2) | ||
|
||
let fetchRequest3: NSFetchRequest<NSFetchRequestResult> = Direction.fetchRequest() | ||
let batchDeleteRequest3 = NSBatchDeleteRequest(fetchRequest: fetchRequest3) | ||
_ = try? container.viewContext.execute(batchDeleteRequest3) | ||
|
||
let fetchRequest4: NSFetchRequest<NSFetchRequestResult> = Component.fetchRequest() | ||
let batchDeleteRequest4 = NSBatchDeleteRequest(fetchRequest: fetchRequest4) | ||
_ = try? container.viewContext.execute(batchDeleteRequest4) | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters