-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
74 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
HMH_iOS/HMH_iOS/Global/Application/MidnightTaskScheduler.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | ||
// } | ||
} | ||
} | ||
|
14 changes: 0 additions & 14 deletions
14
.../HMH_iOS/Global/DataModel/ChallengeModel.xcdatamodeld/ChallengeModel.xcdatamodel/contents
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
HMH_iOS/HMH_iOS/Global/DataModel/DailyChallenge+CoreDataClass.swift
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
HMH_iOS/HMH_iOS/Global/DataModel/DailyChallenge+CoreDataProperties.swift
This file was deleted.
Oops, something went wrong.