Created
February 14, 2023 10:06
-
-
Save yefga/296b816378d2e23dabf6753454ee736e to your computer and use it in GitHub Desktop.
Core Data helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CoreData.swift | |
// Themez (iOS) | |
// | |
// Created by yepz on 09/02/23. | |
// | |
import Foundation | |
import CoreData | |
public class CoreDataHelper { | |
// Singleton Instance | |
public static let shared = CoreDataHelper() | |
// Context from persistentContainer | |
lazy var context = persistentContainer.viewContext | |
// Persistent Container | |
lazy var persistentContainer: NSPersistentContainer = { | |
let container = NSPersistentContainer(name: "ThemezData") | |
container.loadPersistentStores(completionHandler: { (_, error) in | |
if let error = error as NSError? { | |
fatalError("Unresolved error \(error), \(error.userInfo)") | |
} | |
}) | |
return container | |
}() | |
var url: URL? { | |
persistentContainer.viewContext.persistentStoreCoordinator?.persistentStores.first?.url | |
} | |
public func getStoredSize(forPersistentContainerUrl storeUrl: URL) -> CGFloat { | |
do { | |
let size = try Data(contentsOf: storeUrl) | |
if size.count < 1 { | |
printd(type: .warning, message: "Size can't be determined") | |
return 0 | |
} | |
let bcf = ByteCountFormatter() | |
bcf.allowedUnits = [.useKB, .useMB] // optional: restricts the units to MB only | |
bcf.countStyle = .file | |
let sizeString = bcf.string(fromByteCount: Int64(size.count)) | |
let countString = "\(sizeString.components(separatedBy: " ").first ?? "")" | |
guard let count = NumberFormatter().number(from: countString) else { | |
return 0 | |
} | |
let storedSize = CGFloat(truncating: count) | |
printd(type: .success, message: "get size \(storedSize)") | |
return storedSize | |
} catch { | |
printd(type: .error, message: error.localizedDescription) | |
return 0 | |
} | |
} | |
// Save Context | |
public func saveContext() { | |
let context = persistentContainer.viewContext | |
if context.hasChanges { | |
do { | |
try context.save() | |
} catch { | |
let nserror = error as NSError | |
fatalError("Unresolved error \(nserror), \(nserror.userInfo)") | |
} | |
} | |
} | |
// Fetch All Items | |
public func fetchAllItems() -> [NSManagedObject] { | |
let request = NSFetchRequest<NSManagedObject>(entityName: "Item") | |
do { | |
let items = try context.fetch(request) | |
return items | |
} catch let error as NSError { | |
print("Could not fetch. \(error), \(error.userInfo)") | |
return [] | |
} | |
} | |
// Filter by Name | |
public func filterByName(name: String) -> NSManagedObject? { | |
let request = NSFetchRequest<NSManagedObject>(entityName: "Item") | |
request.predicate = NSPredicate(format: "name == %@", name) | |
do { | |
let items = try context.fetch(request) | |
if items.count > 0 { | |
return items[0] | |
} else { | |
return nil | |
} | |
} catch let error as NSError { | |
print("Could not fetch. \(error), \(error.userInfo)") | |
return nil | |
} | |
} | |
// Delete Object Model | |
public func deleteObject(object: NSManagedObject) { | |
context.delete(object) | |
saveContext() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment