Skip to content

Commit

Permalink
[Add/#34] 자정 스케줄러 및 CoreData 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoe0929 committed May 26, 2024
1 parent c3b8dcd commit 785ef91
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 74 deletions.
30 changes: 13 additions & 17 deletions HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -338,10 +337,9 @@
367244E82BF3D770007A5A7B /* AppStorageKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppStorageKey.swift; sourceTree = "<group>"; };
367244EC2BF3E778007A5A7B /* NotificationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationManager.swift; sourceTree = "<group>"; };
367244EF2BF3E8B5007A5A7B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
368123D22C0342A60060DDB7 /* DailyChallenge+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DailyChallenge+CoreDataClass.swift"; sourceTree = "<group>"; };
368123D32C0342A60060DDB7 /* DailyChallenge+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DailyChallenge+CoreDataProperties.swift"; sourceTree = "<group>"; };
368123D72C03432B0060DDB7 /* ChallengeModel.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = ChallengeModel.xcdatamodel; sourceTree = "<group>"; };
368124142C034A6A0060DDB7 /* Persistence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Persistence.swift; sourceTree = "<group>"; };
368124162C0350950060DDB7 /* MidnightTaskScheduler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MidnightTaskScheduler.swift; sourceTree = "<group>"; };
368124212C0353E80060DDB7 /* DailyChallengeModel.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = DailyChallengeModel.xcdatamodel; sourceTree = "<group>"; };
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 = "<group>"; };
368780D92C01ED4E00454827 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -865,16 +863,15 @@
children = (
367244EF2BF3E8B5007A5A7B /* AppDelegate.swift */,
368124142C034A6A0060DDB7 /* Persistence.swift */,
368124162C0350950060DDB7 /* MidnightTaskScheduler.swift */,
);
path = Application;
sourceTree = "<group>";
};
3687555C2C03342600DAF428 /* DataModel */ = {
isa = PBXGroup;
children = (
368123D62C03432B0060DDB7 /* ChallengeModel.xcdatamodeld */,
368123D22C0342A60060DDB7 /* DailyChallenge+CoreDataClass.swift */,
368123D32C0342A60060DDB7 /* DailyChallenge+CoreDataProperties.swift */,
368124202C0353E80060DDB7 /* DailyChallengeModel.xcdatamodeld */,
);
path = DataModel;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand All @@ -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 */,
Expand All @@ -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 */,
Expand Down Expand Up @@ -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 = "<group>";
versionGroupType = wrapper.xcdatamodel;
};
Expand Down
61 changes: 61 additions & 0 deletions HMH_iOS/HMH_iOS/Global/Application/MidnightTaskScheduler.swift
Original file line number Diff line number Diff line change
@@ -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)")
// }
}
}

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 785ef91

Please sign in to comment.