Commit 30dded6d authored by xlu's avatar xlu

added core data save

parent fe5010e8
...@@ -21,6 +21,7 @@ class CDHandler { ...@@ -21,6 +21,7 @@ class CDHandler {
func save(){ func save(){
do { do {
try viewContext.save() try viewContext.save()
print("Save Good")
}catch { }catch {
print(error.localizedDescription) print(error.localizedDescription)
} }
......
...@@ -51,6 +51,22 @@ class ViewController: UIViewController{ ...@@ -51,6 +51,22 @@ class ViewController: UIViewController{
} }
private func saveToCore(productsArray: Array<Product>){
for ele in productsArray{
guard let product = coreDataHandler.add(SavedProduct.self) else { print("failed"); return}
let id = Int32(ele.id)
let brand = ele.brand
let title = ele.title
product.id = id
product.title = title
product.brand = brand
coreDataHandler.save()
}
}
private func getAllProducts(productCompletionHandler: @escaping (ProductStore?, Error?) -> Void){ private func getAllProducts(productCompletionHandler: @escaping (ProductStore?, Error?) -> Void){
guard let url = URL(string: "https://dummyjson.com/products") else{ guard let url = URL(string: "https://dummyjson.com/products") else{
fatalError("URL Failed") fatalError("URL Failed")
......
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="17709" systemVersion="19H1715" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier=""> <model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="17709" systemVersion="19H1715" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="SavedProduct" representedClassName=".SavedProduct" syncable="YES" codeGenerationType="class"> <entity name="SavedProduct" representedClassName=".SavedProduct" syncable="YES" codeGenerationType="class">
<attribute name="attribute" optional="YES"/> <attribute name="brand" optional="YES" attributeType="String" defaultValueString=""/>
<attribute name="brand" optional="YES" attributeType="String"/> <attribute name="category" optional="YES" attributeType="String"/>
<attribute name="descriptionProduct" optional="YES" attributeType="String"/>
<attribute name="discountPercentage" optional="YES" attributeType="Double" usesScalarValueType="NO"/>
<attribute name="id" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/> <attribute name="id" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="title" optional="YES" attributeType="String"/> <attribute name="price" optional="YES" attributeType="Integer 32" usesScalarValueType="YES"/>
<attribute name="rating" optional="YES" attributeType="Double" usesScalarValueType="NO"/>
<attribute name="stock" optional="YES" attributeType="Integer 32" usesScalarValueType="YES"/>
<attribute name="thumbnail" optional="YES" attributeType="String"/>
<attribute name="title" optional="YES" attributeType="String" defaultValueString=""/>
</entity> </entity>
<elements> <elements>
<element name="SavedProduct" positionX="-63" positionY="-18" width="128" height="103"/> <element name="SavedProduct" positionX="-63" positionY="-18" width="128" height="193"/>
</elements> </elements>
</model> </model>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment