- modified the data for live stream url in theme 1 and theme 2 with speicifed languages also with titles - modified FM url - worked on coredata crud. made functions for crud with queries - added default avatar images for guest - Finalised the Data Model and tried to save and update the counts in local
256 lines
7.7 KiB
Swift
256 lines
7.7 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*/
|
|
|
|
enum PersistentStorageEnum : String{
|
|
case UserClicks
|
|
|
|
case click_counts
|
|
case category_id
|
|
case post_id
|
|
case post_type
|
|
}
|
|
|
|
enum PostType: String {
|
|
case series = "1"
|
|
case season = "2"
|
|
case episode = "3"
|
|
case video = "4"
|
|
// case paint = "5"
|
|
case game = "6"
|
|
case audio = "7"
|
|
case karaokeVideo = "8"
|
|
case shopProduct = "9"
|
|
// case parentalVideo = "10"
|
|
// case article = "11"
|
|
case liveTV = "12"
|
|
case FM = "13"
|
|
case teaser = "14"
|
|
case others = "15"
|
|
case home = "16"
|
|
}
|
|
|
|
struct UserClickData {
|
|
let clickCounts: Int
|
|
let categoryId: Int
|
|
let postId: Int
|
|
let postType: Int
|
|
}
|
|
|
|
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 createData(data : UserClickData){
|
|
|
|
//We need to create a context from this container
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
let share = UserClicks(context: managedContext)
|
|
share.click_counts = Int64(data.clickCounts)
|
|
share.category_id = Int64(data.categoryId)
|
|
share.post_id = Int64(data.postId)
|
|
share.post_type = Int64(data.postType)
|
|
|
|
do {
|
|
try managedContext.save()
|
|
retrieveData()
|
|
} catch let error as NSError {
|
|
print("Could not save. \(error), \(error.userInfo)")
|
|
}
|
|
}
|
|
|
|
func checkIfExist( key : PersistentStorageEnum , clicksData : UserClickData) {
|
|
|
|
let managedContext = PersistentStorage.shared.context
|
|
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: PersistentStorageEnum.UserClicks.rawValue)
|
|
// fetchRequest.fetchLimit = 1
|
|
// fetchRequest.predicate = NSPredicate(format: "id == %d" ,id)
|
|
fetchRequest.predicate = NSPredicate(format: "\(key.rawValue) == %@" ,clicksData.postType.toString())
|
|
|
|
do {
|
|
guard let result = try managedContext.fetch(fetchRequest) as? [UserClicks] else {return}
|
|
if result.isEmpty{
|
|
//create
|
|
PersistentStorage.shared.createData(data: clicksData)
|
|
print("create")
|
|
}else{
|
|
//update
|
|
let objectUpdate = result[0] as NSManagedObject
|
|
print("Update")
|
|
objectUpdate.setValue(result.first!.click_counts + Int64(clicksData.clickCounts), forKey: "click_counts")
|
|
do{
|
|
try managedContext.save()
|
|
self.retrieveData()
|
|
}
|
|
catch
|
|
{
|
|
print(error)
|
|
}
|
|
}
|
|
result.forEach { clicks in
|
|
print("Counts" , clicks.click_counts)
|
|
}
|
|
}catch let error as NSError {
|
|
print("Could not fetch. \(error), \(error.userInfo)")
|
|
}
|
|
}
|
|
|
|
func retrieveData() {
|
|
|
|
//We need to create a context from this container
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
|
|
// debugPrint(path[0])
|
|
|
|
let fetchRequests = NSFetchRequest<NSFetchRequestResult>(entityName: "UserClicks")
|
|
|
|
// fetchRequests.fetchLimit = 1
|
|
// fetchRequests.predicate = NSPredicate(format: "uuid = %@", "2")
|
|
// fetchRequests.sortDescriptors = [NSSortDescriptor.init(key: "uuid", ascending: false)]
|
|
|
|
do {
|
|
guard let result = try managedContext.fetch(fetchRequests) as? [UserClicks] else {return}
|
|
result.forEach { clicks in
|
|
print("ID:-" , clicks.post_type, "Count:-", clicks.click_counts)
|
|
}
|
|
|
|
} catch let error
|
|
{
|
|
debugPrint(error)
|
|
}
|
|
}
|
|
|
|
func updateData(){
|
|
//We need to create a context from this container
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
|
|
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "UserClicks")
|
|
fetchRequest.predicate = NSPredicate(format: "uuid = %@", "2")
|
|
do
|
|
{
|
|
let test = try managedContext.fetch(fetchRequest)
|
|
|
|
let objectUpdate = test[0] as! NSManagedObject
|
|
objectUpdate.setValue("Bilal Ahmed Khan New Name", forKey: "name")
|
|
do{
|
|
try managedContext.save()
|
|
self.retrieveData()
|
|
}
|
|
catch
|
|
{
|
|
print(error)
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
print(error)
|
|
}
|
|
|
|
}
|
|
|
|
func deleteData(){
|
|
//We need to create a context from this container
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "UserClicks")
|
|
// fetchRequest.fetchLimit = 1
|
|
// fetchRequest.predicate = NSPredicate(format: "uuid = %@ AND uuid = %@", "1", "1")
|
|
fetchRequest.predicate = NSPredicate(format: "post_id = %@", "11")
|
|
|
|
do
|
|
{
|
|
let test = try managedContext.fetch(fetchRequest)
|
|
|
|
let objectToDelete = test[0] as! NSManagedObject
|
|
// test.forEach { obbject in
|
|
// managedContext.delete(obbject as! NSManagedObject)
|
|
// }
|
|
managedContext.delete(test.first as! NSManagedObject)
|
|
// managedContext.delete(objectToDelete)
|
|
|
|
do{
|
|
try managedContext.save()
|
|
// self.createData()
|
|
self.retrieveData()
|
|
}
|
|
catch
|
|
{
|
|
print(error)
|
|
}
|
|
|
|
}
|
|
catch
|
|
{
|
|
print(error)
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
// }
|
|
// }
|
|
|
|
}
|