Mercurial > public > lazybear
changeset 153:7dde78b4c377
Add Watchlist Core Data class
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Wed, 17 Feb 2021 18:56:13 +0100 |
parents | eeac6822d850 |
children | 0bbf01138b04 |
files | lazybear/LazyBear.xcdatamodeld/LazyBear.xcdatamodel/contents lazybear/Persistence.swift |
diffstat | 2 files changed, 52 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/lazybear/LazyBear.xcdatamodeld/LazyBear.xcdatamodel/contents Wed Feb 17 18:55:27 2021 +0100 +++ b/lazybear/LazyBear.xcdatamodeld/LazyBear.xcdatamodel/contents Wed Feb 17 18:56:13 2021 +0100 @@ -1,4 +1,10 @@ <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="17709" systemVersion="20D74" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier=""> - <elements/> + <entity name="SavedCompanies" representedClassName="SavedCompanies" syncable="YES" codeGenerationType="class"> + <attribute name="name" optional="YES" attributeType="String"/> + <attribute name="symbol" optional="YES" attributeType="String"/> + </entity> + <elements> + <element name="SavedCompanies" positionX="-63" positionY="-18" width="128" height="59"/> + </elements> </model> \ No newline at end of file
--- a/lazybear/Persistence.swift Wed Feb 17 18:55:27 2021 +0100 +++ b/lazybear/Persistence.swift Wed Feb 17 18:56:13 2021 +0100 @@ -5,16 +5,51 @@ // Created by Dennis Concepción Martín on 17/2/21. // -import SwiftUI +import CoreData + +struct PersistenceController { + static let shared = PersistenceController() -struct Persistence: View { - var body: some View { - Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) + static var preview: PersistenceController = { + let result = PersistenceController(inMemory: true) + let viewContext = result.container.viewContext + for _ in 0..<2 { + let savedCompanies = SavedCompanies(context: viewContext) + savedCompanies.name = "apple inc" + savedCompanies.symbol = "aapl" + } + do { + try viewContext.save() + } catch { + // Replace this implementation with code to handle the error appropriately. + // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. + let nsError = error as NSError + fatalError("Unresolved error \(nsError), \(nsError.userInfo)") + } + return result + }() + + let container: NSPersistentCloudKitContainer + + init(inMemory: Bool = false) { + container = NSPersistentCloudKitContainer(name: "LazyBear") + if inMemory { + container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") + } + container.loadPersistentStores(completionHandler: { (storeDescription, error) in + if let error = error as NSError? { + // Replace this implementation with code to handle the error appropriately. + // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. + /* + Typical reasons for an error here include: + * The parent directory does not exist, cannot be created, or disallows writing. + * The persistent store is not accessible, due to permissions or data protection when the device is locked. + * The device is out of space. + * The store could not be migrated to the current model version. + Check the error message to determine what the actual problem was. + */ + fatalError("Unresolved error \(error), \(error.userInfo)") + } + }) } } - -struct Persistence_Previews: PreviewProvider { - static var previews: some View { - Persistence() - } -}