Commit fe5010e8 authored by xlu's avatar xlu

added feature fetching from url

parent e6b4df42
File added
......@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
262804D128498A5A000E1A24 /* CDHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 262804D028498A5A000E1A24 /* CDHandler.swift */; };
2672E4CD2841747500D1D02C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2672E4CC2841747500D1D02C /* AppDelegate.swift */; };
2672E4CF2841747500D1D02C /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2672E4CE2841747500D1D02C /* SceneDelegate.swift */; };
2672E4D12841747500D1D02C /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2672E4D02841747500D1D02C /* ViewController.swift */; };
......@@ -18,6 +19,7 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
262804D028498A5A000E1A24 /* CDHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CDHandler.swift; sourceTree = "<group>"; };
2672E4C92841747500D1D02C /* iOSNetworkJSON.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = iOSNetworkJSON.app; sourceTree = BUILT_PRODUCTS_DIR; };
2672E4CC2841747500D1D02C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
2672E4CE2841747500D1D02C /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
......@@ -69,6 +71,7 @@
2672E4DA2841747600D1D02C /* LaunchScreen.storyboard */,
2672E4DD2841747600D1D02C /* Info.plist */,
2672E4D52841747500D1D02C /* iOSNetworkJSON.xcdatamodeld */,
262804D028498A5A000E1A24 /* CDHandler.swift */,
);
path = iOSNetworkJSON;
sourceTree = "<group>";
......@@ -143,6 +146,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
262804D128498A5A000E1A24 /* CDHandler.swift in Sources */,
2672E4D12841747500D1D02C /* ViewController.swift in Sources */,
2672E4CD2841747500D1D02C /* AppDelegate.swift in Sources */,
26EE2CCF28419B02004C74AF /* ProductTableViewCell.swift in Sources */,
......
......@@ -27,16 +27,25 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="120"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="F0x-Vp-2XT">
<rect key="frame" x="53" y="43" width="97" height="34"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="F0x-Vp-2XT">
<rect key="frame" x="53" y="21" width="308" height="56"/>
<constraints>
<constraint firstAttribute="height" constant="34" id="2mV-m0-yp4"/>
<constraint firstAttribute="width" constant="200" id="tBl-ow-DqZ"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
</subviews>
<constraints>
<constraint firstAttribute="trailingMargin" secondItem="F0x-Vp-2XT" secondAttribute="trailing" constant="33" id="537-Qt-N2b"/>
<constraint firstAttribute="bottomMargin" secondItem="F0x-Vp-2XT" secondAttribute="bottom" constant="32" id="EEb-so-rrb"/>
<constraint firstItem="F0x-Vp-2XT" firstAttribute="top" secondItem="ed9-dy-DXb" secondAttribute="topMargin" constant="10" id="WLa-XA-XUs"/>
<constraint firstItem="F0x-Vp-2XT" firstAttribute="leading" secondItem="ed9-dy-DXb" secondAttribute="leadingMargin" constant="33" id="cH9-lt-Egl"/>
</constraints>
</tableViewCellContentView>
<connections>
<outlet property="ProductDescription" destination="F0x-Vp-2XT" id="lo4-Q5-Rwu"/>
<outlet property="ProductDescription" destination="F0x-Vp-2XT" id="KTu-S0-rEO"/>
</connections>
</tableViewCell>
</prototypes>
......
//
// CDHandler.swift
// iOSNetworkJSON
//
// Created by xlu on 6/2/22.
//
import UIKit
import CoreData
class CDHandler {
private let viewContext: NSManagedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
func add<T: NSManagedObject>(_ type: T.Type) -> T? {
guard let entityName = T.entity().name else {return nil}
guard let entity = NSEntityDescription.entity(forEntityName: entityName, in: viewContext) else {return nil}
let object = T(entity: entity, insertInto: viewContext)
return object
}
func save(){
do {
try viewContext.save()
}catch {
print(error.localizedDescription)
}
}
}
......@@ -6,6 +6,7 @@
//
import UIKit
import CoreData
struct ProductStore: Codable {
......@@ -31,17 +32,26 @@ struct Product: Codable {
class ViewController: UIViewController{
let coreDataHandler = CDHandler()
@IBOutlet weak var tableView: UITableView!
var allProducts = [Product]()
override func viewDidLoad() {
super.viewDidLoad()
getAllProducts()
getAllProducts(productCompletionHandler: {productall, error in
if let products = productall?.products{
self.allProducts = products
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
})
}
private func getAllProducts(){
private func getAllProducts(productCompletionHandler: @escaping (ProductStore?, Error?) -> Void){
guard let url = URL(string: "https://dummyjson.com/products") else{
fatalError("URL Failed")
}
......@@ -51,13 +61,8 @@ class ViewController: UIViewController{
guard let productall = try? JSONDecoder().decode(ProductStore.self, from: data)else{
fatalError("decode error")
}
for element in productall.products {
self.allProducts.append(element)
}
}
productCompletionHandler(productall, nil)
DispatchQueue.main.async { [self] in
tableView.reloadData()
}
}.resume()
}
......
<?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="">
<entity name="Entity" representedClassName="Entity" 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"/>
<attribute name="id" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="title" optional="YES" attributeType="String"/>
</entity>
<elements>
<element name="Entity" positionX="-63" positionY="-18" width="128" height="43"/>
<element name="SavedProduct" positionX="-63" positionY="-18" width="128" height="103"/>
</elements>
</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