feat: set up persistence

This commit is contained in:
dscyrescotti
2024-05-04 21:33:50 +07:00
parent a6b48af947
commit f0fbb7263a
26 changed files with 177 additions and 4 deletions
+66
View File
@@ -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)")
}
}()
}
+20
View File
@@ -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

@@ -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>