Skip to main content

Custom Loader (Activity Indicator) in Swift

Creating a custom activity indicator in Swift allows you to tailor the appearance and behavior of your loading spinner to fit the style of your app. Here's a step-by-step guide to creating a simple custom activity indicator using UIView Step 1: Create a New Swift File for the Custom Activity Indicator Create a new Swift file and name it  RotatingCirclesView.swift . Add the following code to define a custom UIView subclass for your activity indicator: // //   RotatingCirclesView.swift //   Welcome In // //   Created by Praveen Kumar on 05/09/24. // import UIKit class RotatingCirclesView : UIView {          let circle1 = UIView ( frame : CGRect ( x : 20 , y : 20 , width : 60 , height : 60 ))     let circle2 = UIView ( frame : CGRect ( x : 120 , y : 20 , width : 60 , height : 60 ))          let position : [ CGRect ] = [ CGRect ( x : 30 , y : 20 , width : 60 , height : 60 ), CGRect ( x : 60 , y : 15 ,...

How to Save, Fetch, Update and Delete data from Core Data using Swift 5.0

 

Getting Started Save, Fetch, Update and Delete Core Data in Swift 5.0

Core Data is NOT an SQLite database. it is a graphical and persistence framework.CURD in swift (Create, Update, Retrieve, Delete)

CURD in swift (Create, Update, Retrieve, Delete)


//MARK: - Save User Info Data

func addData(user_nameStringuser_emailStringuser_dobStringimageDataNSData) {

        

        guard let appDelegate = UIApplication.shared.delegate asAppDelegate else {

            return

        }

        let managedContext  = appDelegate.persistentContainer.viewContext

        let entity = NSEntityDescription.entity(forEntityName"UserInfo"in: managedContext)!

        

        let UserData = NSManagedObject(entity: entity, insertInto: managedContext)

        UserData.setValue(user_name, forKeyPath"user_name")

        UserData.setValue(user_email, forKeyPath"user_email")

        UserData.setValue(user_dob, forKeyPath"user_dob")

        UserData.setValue(imageData, forKeyPath"image")

        do {

            try managedContext.save()

            getUserInfo.append(UserData)

            print("save data.")

        catch let error as NSError {

            print("Could not save. \(error)\(error.userInfo)")

        }

    }


//MARK: - Fetch User Info Data


    func fetchData(){

        let context = (UIApplication.shared.delegate as!                 AppDelegate).persistentContainer.viewContext

        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "UserInfo")

        do{

            let results = try context.fetch(fetchRequest)

            let  dateCreated = results as! [NSManagedObject]

            for _datecreated in dateCreated {

                userListData.append(_datecreated)

                tableView.reloadData()

            }

        }catch let err as NSError {

            print(err.debugDescription)

        }

    }



//MARK: - Delete User Info Data



    func deleteInfo(){

        let appDelegate = UIApplication.shared.delegate as! AppDelegate

        let context = appDelegate.persistentContainer.viewContext

        let requestDel = NSFetchRequest<NSFetchRequestResult>(entityName: "UserInfo")

        requestDel.returnsObjectsAsFaults = false

        let predicate = NSPredicate(format: "(user_name = %@)", deleteValue)

        requestDel.predicate = predicate

           do {

                let arrUsrObj = try context.fetch(requestDel)

                for usrObj in arrUsrObj as! [NSManagedObject] { // Fetching Object

                    context.delete(usrObj) // Deleting Object

               }

           } catch {

                print("Failed")

           }

           do {

               try context.save()

           } catch {

               print("Failed saving")

           }

    }



//MARK: - Update User Info Data


    func UpdateInfo(){

        let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

        let entity = NSEntityDescription.entity(forEntityName: "UserInfo", in: managedContext)

                let request = NSFetchRequest<NSFetchRequestResult>()

                request.entity = entity

                let predicate = NSPredicate(format: "(user_name = %@)", userNameStr)

                request.predicate = predicate

                do {

                    let results =

                        try managedContext.fetch(request)

                    let objectUpdate = results[0] as! NSManagedObject

                    objectUpdate.setValue(userName_textField.text!, forKey: "user_name")

                    objectUpdate.setValue(userEmail_textField.text!, forKey: "user_email")

                    objectUpdate.setValue(userDOB_textField.text!, forKey: "user_dob")

                    do {

                        try managedContext.save()

                        userName_textField.text = ""

                        userEmail_textField.text = ""

                        userDOB_textField.text = ""

                        print("Update")

                    }catch _ as NSError {

                      print("Update")

                    }

                }

                catch _ as NSError {

                    print("Update")

                }

    }

Comments