VesselWheel

러닝기록 타이머를 정지하며 코어데이터에 저장하기(작성중) 본문

Xcode Study

러닝기록 타이머를 정지하며 코어데이터에 저장하기(작성중)

JasonYang 2024. 3. 4. 22:03

Run_It.xcdatamodel에 저장된 Entities와 하부 attributes

 

코어데이터를 활용하기 위한 CoreDataManager

더보기
//
//  CoreDataManager.swift
//  Run-It
//
//  Created by t2023-m0024 on 2/29/24.
//

import Foundation
import CoreData
import KakaoSDKUser

class CoreDataManager {
    static let shared = CoreDataManager()
    
    private init() {}

    // MARK: - Core Data stack
    lazy var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentContainer(name: "Run_It")
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                print("Unresolved error \(error), \(error.userInfo)")
                // Handle the error appropriately
            }
        })
        return container
    }()
    
    // MARK: - Core Data Saving support
    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)")
            }
        }
    }
    
    // MARK: - User Operations
    func createUser(email: String, name: String, profilePhoto: Data) -> User? {
        let context = persistentContainer.viewContext
        // Correct way to create a new User entity
        guard let entity = NSEntityDescription.entity(forEntityName: "User", in: context) else {
            print("Failed to create entity description for User")
            return nil
        }

        let user = User(entity: entity, insertInto: context)
        user.userId = UUID()
        user.email = email
        user.name = name
        user.createdAt = Date()
        // Assume profilePhoto is stored as Data
        user.profilePhoto = profilePhoto
        
        do {
            try context.save()
            return user
        } catch {
            print("Failed to create user: \(error)")
            return nil
        }
    }

    
    // Add similar functions for other CRUD operations on User and other entities
    // For example: fetchUsers(), updateUser(), deleteUser(), etc.
    
    // MARK: - Destination Operations
    // Add functions for creating, reading, updating, and deleting destinations
    
    // MARK: - Favorite Operations
    // Add functions for managing favorites
    
    // MARK: - PlaceInfo Operations
    // Add functions for managing place information
    
    // MARK: - RunningRecord Operations
    func createRunningRecord(time: Int, distance: Double, pace: Double) -> RunningRecord? {
        let context = persistentContainer.viewContext
        guard let entity = NSEntityDescription.entity(forEntityName: "RunningRecord", in: context) else {
            print("Failed to create entity description for RunningRecord")
            return nil
        }

        let record = RunningRecord(entity: entity, insertInto: context)
//        record.recordId = UUID()
        record.id =  UUID()
        record.time = Int32(time)
        record.distance = distance
        record.pace = pace
//        record.createdAt = Date()
        print("CoreData id: \(String(describing: record.id)) Time: \(record.time), Distance: \(record.distance), Pace: \(record.pace)")
        
        do {
            try context.save()
            return record
        } catch {
            print("Failed to create running record: \(error)")
            return nil
        }
    }

    
    // MARK: - RunningRecord Operations
    func fetchRunningRecords() -> [RunningRecord] {
        let context = persistentContainer.viewContext
        let fetchRequest: NSFetchRequest<RunningRecord> = RunningRecord.fetchRequest()

        do {
            let records = try context.fetch(fetchRequest)
            return records
        } catch {
            print("Failed to fetch running records: \(error)")
            return []
        }
    }
}

 

    @objc private func stopRunning() {
        print("TappedButton - stopRunning()")
        print("stop Time: \(self.time), Distance: \(self.distance), Pace: \(self.pace)")
        let alert = UIAlertController(title: "운동을 완료하시겠습니까?", message: "근처 편의점에서 물 한잔 어떻신가요?", preferredStyle: .alert)
        
        alert.addAction(UIAlertAction(title: "운동 완료하기", style: .default, handler: { _ in
            self.runningTimer.stop()
            CoreDataManager.shared.createRunningRecord(time: self.time, distance: self.distance, pace: self.pace)


//            let records = CoreDataManager.shared.fetchRunningRecords()
//            for record in records {
//                print("CoreData Time: \(record.time), Distance: \(record.distance), Pace: \(record.pace)")
//            }
            let mainTabBarViewController =  MainTabBarViewController()
            mainTabBarViewController.modalPresentationStyle = .fullScreen
            self.present(mainTabBarViewController, animated: true)
        }))
        
        alert.addAction(UIAlertAction(title: "취소하기", style: .destructive, handler: nil))

        self.present(alert, animated: true, completion: nil)
    }

 

 

저장된 코어데이터를 콘솔창에서 출력