Files
Woka_Native_iOS/WOKA/PersistentStorage.swift
2024-08-05 19:43:30 +05:30

74 lines
2.0 KiB
Swift

//
// PersistentStorage.swift
// WOKA
//
// Created by MacBook Pro on 05/08/24.
//
import Foundation
import CoreData
/**
Hey there, I hope you enjoyed the video, if you have any questions then please feel free to ask I will be happy to answer them.
Do share this with your iOS group on whatsapp or facebook or anyone who wants to learn iOS*/
final class PersistentStorage
{
private init(){}
static let shared = PersistentStorage()
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "WOKA")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved errorsss \(error), \(error.userInfo)")
}
})
return container
}()
lazy var context = persistentContainer.viewContext
// MARK: - Core Data Saving support
func saveContext() {
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved errorsss \(nserror), \(nserror.userInfo)")
}
}
}
func saveClicksCount(){
let clicks = UserClicks(context: PersistentStorage.shared.context)
clicks.post_type = 1
clicks.post_id = 22
clicks.category_id = 33
clicks.click_counts = 100
PersistentStorage.shared.saveContext()
}
func getchClicksCount(){
let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
debugPrint(path[0])
do {
guard let result = try PersistentStorage.shared.context.fetch(UserClicks.fetchRequest()) as? [UserClicks] else {return}
result.forEach({debugPrint("sad", $0.click_counts)})
} catch let error
{
debugPrint(error)
}
}
}