feat: set up persistence
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// Persistence.swift
|
||||
// Memola
|
||||
//
|
||||
// Created by Dscyre Scotti on 5/4/24.
|
||||
//
|
||||
|
||||
import CoreData
|
||||
import Foundation
|
||||
|
||||
class Persistence {
|
||||
private let modelName = "MemolaModel"
|
||||
|
||||
static let shared: Persistence = Persistence()
|
||||
|
||||
private init() { }
|
||||
|
||||
var viewContext: NSManagedObjectContext {
|
||||
persistentContainer.viewContext
|
||||
}
|
||||
|
||||
lazy var persistentContainer: NSPersistentContainer = {
|
||||
let persistentStore = NSPersistentStoreDescription()
|
||||
persistentStore.shouldMigrateStoreAutomatically = true
|
||||
persistentStore.shouldInferMappingModelAutomatically = true
|
||||
let container = NSPersistentContainer(name: modelName, managedObjectModel: managedObjectModel)
|
||||
do {
|
||||
let coordinator = container.persistentStoreCoordinator
|
||||
if let oldStore = coordinator.persistentStores.first {
|
||||
try coordinator.remove(oldStore)
|
||||
}
|
||||
_ = try coordinator.addPersistentStore(type: .sqlite, at: sqliteURL)
|
||||
} catch {
|
||||
fatalError("[Memola] - \(error.localizedDescription)")
|
||||
}
|
||||
container.persistentStoreDescriptions = [persistentStore]
|
||||
container.loadPersistentStores { description, error in
|
||||
if let error {
|
||||
fatalError("[Memola]: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
return container
|
||||
}()
|
||||
|
||||
private lazy var managedObjectModel: NSManagedObjectModel = {
|
||||
guard let modelURL = Bundle.main.url(forResource: modelName, withExtension: ".momd"), let model = NSManagedObjectModel(contentsOf: modelURL) else {
|
||||
fatalError("[Memola]: Unable to load model.")
|
||||
}
|
||||
return model
|
||||
}()
|
||||
|
||||
private lazy var sqliteURL: URL = {
|
||||
do {
|
||||
let fileURL = try FileManager.default.url(
|
||||
for: .documentDirectory,
|
||||
in: .userDomainMask,
|
||||
appropriateFor: nil,
|
||||
create: true
|
||||
).appendingPathComponent("\(modelName).sqlite")
|
||||
NSLog(fileURL.absoluteString)
|
||||
return fileURL
|
||||
} catch {
|
||||
fatalError("[Memola]: \(error.localizedDescription)")
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// Memo.swift
|
||||
// Memola
|
||||
//
|
||||
// Created by Dscyre Scotti on 5/4/24.
|
||||
//
|
||||
|
||||
import CoreData
|
||||
import Foundation
|
||||
|
||||
@objc(Memo)
|
||||
class Memo: NSManagedObject {
|
||||
@NSManaged var id: UUID
|
||||
@NSManaged var title: String
|
||||
@NSManaged var data: Data
|
||||
@NSManaged var createdAt: Date
|
||||
@NSManaged var updatedAt: Date
|
||||
}
|
||||
|
||||
extension Memo: Identifiable { }
|
||||
|
Before Width: | Height: | Size: 3.7 KiB After Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 23 KiB |
|
Before Width: | Height: | Size: 3.4 KiB After Width: | Height: | Size: 3.4 KiB |
|
Before Width: | Height: | Size: 9.6 KiB After Width: | Height: | Size: 9.6 KiB |
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
|
Before Width: | Height: | Size: 427 B After Width: | Height: | Size: 427 B |
|
Before Width: | Height: | Size: 859 B After Width: | Height: | Size: 859 B |
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 3.5 KiB |
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22222" systemVersion="23B74" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithSwiftData="YES" userDefinedModelVersionIdentifier="">
|
||||
<entity name="Memo" representedClassName="Memo" syncable="YES">
|
||||
<attribute name="createdAt" attributeType="Date" usesScalarValueType="NO"/>
|
||||
<attribute name="data" attributeType="Binary" allowsExternalBinaryDataStorage="YES"/>
|
||||
<attribute name="id" attributeType="UUID" usesScalarValueType="NO"/>
|
||||
<attribute name="title" attributeType="String"/>
|
||||
<attribute name="updatedAt" attributeType="Date" usesScalarValueType="NO"/>
|
||||
</entity>
|
||||
</model>
|
||||