diff --git a/HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj b/HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj index d7aaaf2f..c9424252 100644 --- a/HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj +++ b/HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj @@ -129,10 +129,9 @@ 367244ED2BF3E778007A5A7B /* NotificationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 367244EC2BF3E778007A5A7B /* NotificationManager.swift */; }; 367244F02BF3E8B5007A5A7B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 367244EF2BF3E8B5007A5A7B /* AppDelegate.swift */; }; 367244F22BF3EA51007A5A7B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 365CD6C02B7E4C5100245CDD /* Assets.xcassets */; }; - 368123D42C0342A60060DDB7 /* DailyChallenge+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 368123D22C0342A60060DDB7 /* DailyChallenge+CoreDataClass.swift */; }; - 368123D52C0342A60060DDB7 /* DailyChallenge+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 368123D32C0342A60060DDB7 /* DailyChallenge+CoreDataProperties.swift */; }; - 368123D82C03432C0060DDB7 /* ChallengeModel.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 368123D62C03432B0060DDB7 /* ChallengeModel.xcdatamodeld */; }; 368124152C034A6A0060DDB7 /* Persistence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 368124142C034A6A0060DDB7 /* Persistence.swift */; }; + 368124172C0350950060DDB7 /* MidnightTaskScheduler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 368124162C0350950060DDB7 /* MidnightTaskScheduler.swift */; }; + 368124222C0353E80060DDB7 /* DailyChallengeModel.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 368124202C0353E80060DDB7 /* DailyChallengeModel.xcdatamodeld */; }; 368780D42C01ED4E00454827 /* ManagedSettings.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 368CAABC2BB961D900FA83B3 /* ManagedSettings.framework */; }; 368780D52C01ED4E00454827 /* ManagedSettingsUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3672442A2BF31D5F007A5A7B /* ManagedSettingsUI.framework */; }; 368780D82C01ED4E00454827 /* ShieldConfigurationExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 368780D72C01ED4E00454827 /* ShieldConfigurationExtension.swift */; }; @@ -338,10 +337,9 @@ 367244E82BF3D770007A5A7B /* AppStorageKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppStorageKey.swift; sourceTree = ""; }; 367244EC2BF3E778007A5A7B /* NotificationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationManager.swift; sourceTree = ""; }; 367244EF2BF3E8B5007A5A7B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; - 368123D22C0342A60060DDB7 /* DailyChallenge+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DailyChallenge+CoreDataClass.swift"; sourceTree = ""; }; - 368123D32C0342A60060DDB7 /* DailyChallenge+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DailyChallenge+CoreDataProperties.swift"; sourceTree = ""; }; - 368123D72C03432B0060DDB7 /* ChallengeModel.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = ChallengeModel.xcdatamodel; sourceTree = ""; }; 368124142C034A6A0060DDB7 /* Persistence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Persistence.swift; sourceTree = ""; }; + 368124162C0350950060DDB7 /* MidnightTaskScheduler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MidnightTaskScheduler.swift; sourceTree = ""; }; + 368124212C0353E80060DDB7 /* DailyChallengeModel.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = DailyChallengeModel.xcdatamodel; sourceTree = ""; }; 368780D32C01ED4E00454827 /* ShieldConfigureExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = ShieldConfigureExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; }; 368780D72C01ED4E00454827 /* ShieldConfigurationExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShieldConfigurationExtension.swift; sourceTree = ""; }; 368780D92C01ED4E00454827 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -865,6 +863,7 @@ children = ( 367244EF2BF3E8B5007A5A7B /* AppDelegate.swift */, 368124142C034A6A0060DDB7 /* Persistence.swift */, + 368124162C0350950060DDB7 /* MidnightTaskScheduler.swift */, ); path = Application; sourceTree = ""; @@ -872,9 +871,7 @@ 3687555C2C03342600DAF428 /* DataModel */ = { isa = PBXGroup; children = ( - 368123D62C03432B0060DDB7 /* ChallengeModel.xcdatamodeld */, - 368123D22C0342A60060DDB7 /* DailyChallenge+CoreDataClass.swift */, - 368123D32C0342A60060DDB7 /* DailyChallenge+CoreDataProperties.swift */, + 368124202C0353E80060DDB7 /* DailyChallengeModel.xcdatamodeld */, ); path = DataModel; sourceTree = ""; @@ -1198,10 +1195,7 @@ 365CD6BD2B7E4C4F00245CDD /* HMH_iOSApp.swift in Sources */, 367244062BEDEC4A007A5A7B /* ChallengeRouter.swift in Sources */, 0BD2D67C2B9F5A7B00733843 /* String.swift in Sources */, - 368123D42C0342A60060DDB7 /* DailyChallenge+CoreDataClass.swift in Sources */, - 368123D52C0342A60060DDB7 /* DailyChallenge+CoreDataProperties.swift in Sources */, 367244112BF08684007A5A7B /* BaseModel.swift in Sources */, - 368123D82C03432C0060DDB7 /* ChallengeModel.xcdatamodeld in Sources */, 0BC0E5BD2BB0571E00FB0330 /* NextButtonView.swift in Sources */, 367244ED2BF3E778007A5A7B /* NotificationManager.swift in Sources */, 367244152BF0956B007A5A7B /* PointView.swift in Sources */, @@ -1226,6 +1220,7 @@ 0BF56C932BC39127003ECFB1 /* PickerView.swift in Sources */, 367243D12BEDE9F6007A5A7B /* NetworkHelper.swift in Sources */, 367243FD2BEDEB32007A5A7B /* EmptyResponseDTO.swift in Sources */, + 368124172C0350950060DDB7 /* MidnightTaskScheduler.swift in Sources */, 367244E92BF3D770007A5A7B /* AppStorageKey.swift in Sources */, 367243FB2BEDEB32007A5A7B /* CreateChallengeRequestDTO.swift in Sources */, 0BBF28862BF2F84700101F40 /* WithdrawAlertView.swift in Sources */, @@ -1234,6 +1229,7 @@ 0B9834DC2BC8234700A1457A /* SplashView.swift in Sources */, 367243E32BEDEAF8007A5A7B /* SocialLoginResponseDTO.swift in Sources */, 3601C8382BC6EC5C005790F7 /* UsageTimeListItemView.swift in Sources */, + 368124222C0353E80060DDB7 /* DailyChallengeModel.xcdatamodeld in Sources */, 367243FA2BEDEB32007A5A7B /* GetHomeChallengeResponseDTO.swift in Sources */, 367244072BEDEC4A007A5A7B /* MyPageRouter.swift in Sources */, 0BC0E5B12BB04BA800FB0330 /* SurveyProblemView.swift in Sources */, @@ -1921,14 +1917,14 @@ /* End XCSwiftPackageProductDependency section */ /* Begin XCVersionGroup section */ - 368123D62C03432B0060DDB7 /* ChallengeModel.xcdatamodeld */ = { + 368124202C0353E80060DDB7 /* DailyChallengeModel.xcdatamodeld */ = { isa = XCVersionGroup; children = ( - 368123D72C03432B0060DDB7 /* ChallengeModel.xcdatamodel */, + 368124212C0353E80060DDB7 /* DailyChallengeModel.xcdatamodel */, ); - currentVersion = 368123D72C03432B0060DDB7 /* ChallengeModel.xcdatamodel */; - name = ChallengeModel.xcdatamodeld; - path = /Users/ljh/Desktop/HMH/HMH_iOS/HMH_iOS/HMH_iOS/Global/DataModel/ChallengeModel.xcdatamodeld; + currentVersion = 368124212C0353E80060DDB7 /* DailyChallengeModel.xcdatamodel */; + name = DailyChallengeModel.xcdatamodeld; + path = /Users/ljh/Desktop/HMH/HMH_iOS/HMH_iOS/HMH_iOS/Global/DataModel/DailyChallengeModel.xcdatamodeld; sourceTree = ""; versionGroupType = wrapper.xcdatamodel; }; diff --git a/HMH_iOS/HMH_iOS/Global/Application/MidnightTaskScheduler.swift b/HMH_iOS/HMH_iOS/Global/Application/MidnightTaskScheduler.swift new file mode 100644 index 00000000..7894cf1d --- /dev/null +++ b/HMH_iOS/HMH_iOS/Global/Application/MidnightTaskScheduler.swift @@ -0,0 +1,61 @@ +// +// MidnightTaskScheduler.swift +// HMH_iOS +// +// Created by 이지희 on 5/26/24. +// + +import BackgroundTasks +import CoreData + +class MidnightTaskScheduler { + var timer: Timer? + var persistentContainer = PersistenceController.shared + + init(container: PersistenceController) { + self.persistentContainer = container + BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.example.app.refresh", using: nil) { task in + self.handleAppRefresh(task: task as! BGAppRefreshTask) + } + scheduleMidnightTask() + } + + func scheduleMidnightTask() { + let request = BGAppRefreshTaskRequest(identifier: "com.example.app.refresh") + request.earliestBeginDate = nextMidnight() // 다음 자정에 실행되도록 설정 + + do { + try BGTaskScheduler.shared.submit(request) + } catch { + print("Unable to submit task: \(error)") + } + } + + func handleAppRefresh(task: BGAppRefreshTask) { + // 데이터 저장 작업 실행 + saveDataToLocalDatabase() + + // 작업이 끝나면 새로 예약 + scheduleMidnightTask() + + task.setTaskCompleted(success: true) + } + + func nextMidnight() -> Date { + let now = Date() + var calendar = Calendar.current + calendar.timeZone = TimeZone.current + + let nextMidnight = calendar.nextDate(after: now, matching: DateComponents(hour: 0, minute: 0, second: 0), matchingPolicy: .nextTime)! + return nextMidnight + } + +// do { +// try context.save() +// print("데이터 저장 성공") +// } catch { +// print("데이터 저장 실패: \(error)") +// } + } +} + diff --git a/HMH_iOS/HMH_iOS/Global/DataModel/ChallengeModel.xcdatamodeld/ChallengeModel.xcdatamodel/contents b/HMH_iOS/HMH_iOS/Global/DataModel/ChallengeModel.xcdatamodeld/ChallengeModel.xcdatamodel/contents deleted file mode 100644 index 65c9b579..00000000 --- a/HMH_iOS/HMH_iOS/Global/DataModel/ChallengeModel.xcdatamodeld/ChallengeModel.xcdatamodel/contents +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/HMH_iOS/HMH_iOS/Global/DataModel/DailyChallenge+CoreDataClass.swift b/HMH_iOS/HMH_iOS/Global/DataModel/DailyChallenge+CoreDataClass.swift deleted file mode 100644 index e112830b..00000000 --- a/HMH_iOS/HMH_iOS/Global/DataModel/DailyChallenge+CoreDataClass.swift +++ /dev/null @@ -1,15 +0,0 @@ -// -// DailyChallenge+CoreDataClass.swift -// HMH_iOS -// -// Created by 이지희 on 5/26/24. -// -// - -import Foundation -import CoreData - -@objc(DailyChallenge) -public class DailyChallengeModel: NSManagedObject { - -} diff --git a/HMH_iOS/HMH_iOS/Global/DataModel/DailyChallenge+CoreDataProperties.swift b/HMH_iOS/HMH_iOS/Global/DataModel/DailyChallenge+CoreDataProperties.swift deleted file mode 100644 index 0b8d102f..00000000 --- a/HMH_iOS/HMH_iOS/Global/DataModel/DailyChallenge+CoreDataProperties.swift +++ /dev/null @@ -1,28 +0,0 @@ -// -// DailyChallenge+CoreDataProperties.swift -// HMH_iOS -// -// Created by 이지희 on 5/26/24. -// -// - -import Foundation -import CoreData - - -extension DailyChallengeModel { - - @nonobjc public class func fetchRequest() -> NSFetchRequest { - return NSFetchRequest(entityName: "DailyChallenge") - } - - @NSManaged public var date: String? - @NSManaged public var is_send: Bool - @NSManaged public var challenge: NSObject? - @NSManaged public var app_data: AppData? - -} - -extension DailyChallengeModel : Identifiable { - -}