Fix profile upate task

This commit is contained in:
世界
2023-09-22 18:00:19 +08:00
parent 3374f8e727
commit 58bc9baa05
5 changed files with 33 additions and 21 deletions
@@ -5,16 +5,21 @@ import Library
#if os(iOS) || os(tvOS) #if os(iOS) || os(tvOS)
public class UIProfileUpdateTask: BGAppRefreshTask { public class UIProfileUpdateTask: BGAppRefreshTask {
private static let taskSchedulerPermittedIdentifier = "\(FilePath.packageName).update_profiles" private static let taskSchedulerPermittedIdentifier = "\(FilePath.packageName).update_profiles"
private static var registered = false
public static func configure() async throws { public static func configure() async throws {
let success = BGTaskScheduler.shared.register(forTaskWithIdentifier: taskSchedulerPermittedIdentifier, using: nil) { task in if !registered {
NSLog("profile update task started") let success = BGTaskScheduler.shared.register(forTaskWithIdentifier: taskSchedulerPermittedIdentifier, using: nil) { task in
Task { NSLog("profile update task started")
await getAndupdateProfiles(task) Task {
await getAndupdateProfiles(task)
}
} }
} if !success {
if !success { throw NSError(domain: "register failed", code: 0)
throw NSError(domain: "register failed", code: 0) }
registered = true
} }
BGTaskScheduler.shared.cancelAllTaskRequests() BGTaskScheduler.shared.cancelAllTaskRequests()
let profiles = try await ProfileManager.listAutoUpdateEnabled() let profiles = try await ProfileManager.listAutoUpdateEnabled()
@@ -170,11 +170,6 @@ public struct EditProfileView: View {
do { do {
try await Task.sleep(nanoseconds: UInt64(100 * Double(NSEC_PER_MSEC))) try await Task.sleep(nanoseconds: UInt64(100 * Double(NSEC_PER_MSEC)))
try await profile.updateRemoteProfile() try await profile.updateRemoteProfile()
#if os(iOS) || os(tvOS)
try await UIProfileUpdateTask.configure()
#else
try await ProfileUpdateTask.configure()
#endif
} catch { } catch {
alert = Alert(error) alert = Alert(error)
} }
@@ -194,6 +189,11 @@ public struct EditProfileView: View {
private func saveProfile() async { private func saveProfile() async {
do { do {
_ = try await ProfileManager.update(profile) _ = try await ProfileManager.update(profile)
#if os(iOS) || os(tvOS)
try await UIProfileUpdateTask.configure()
#else
try await ProfileUpdateTask.configure()
#endif
} catch { } catch {
alert = Alert(error) alert = Alert(error)
return return
@@ -247,5 +247,12 @@ public struct NewProfileView: View {
autoUpdateInterval: autoUpdateInterval, autoUpdateInterval: autoUpdateInterval,
lastUpdated: lastUpdated lastUpdated: lastUpdated
)) ))
if profileType == .remote {
#if os(iOS) || os(tvOS)
try await UIProfileUpdateTask.configure()
#else
try await ProfileUpdateTask.configure()
#endif
}
} }
} }
+7 -7
View File
@@ -18,19 +18,19 @@ class ApplicationDelegate: NSObject, UIApplicationDelegate {
} }
private func setup() async { private func setup() async {
await setupBackground()
if UIDevice.current.userInterfaceIdiom == .phone {
await requestNetworkPermission()
}
}
private nonisolated func setupBackground() async {
do { do {
try await UIProfileUpdateTask.configure() try await UIProfileUpdateTask.configure()
NSLog("setup background task success") NSLog("setup background task success")
} catch { } catch {
NSLog("setup background task error: \(error.localizedDescription)") NSLog("setup background task error: \(error.localizedDescription)")
} }
if UIDevice.current.userInterfaceIdiom == .phone {
await requestNetworkPermission()
}
await setupBackground()
}
private nonisolated func setupBackground() async {
if #available(iOS 16.0, *) { if #available(iOS 16.0, *) {
do { do {
let profileServer = try ProfileServer() let profileServer = try ProfileServer()
+2 -2
View File
@@ -9,12 +9,12 @@ class ApplicationDelegate: NSObject, UIApplicationDelegate {
NSLog("Here I stand") NSLog("Here I stand")
LibboxSetup(FilePath.sharedDirectory.relativePath, FilePath.workingDirectory.relativePath, FilePath.cacheDirectory.relativePath, true) LibboxSetup(FilePath.sharedDirectory.relativePath, FilePath.workingDirectory.relativePath, FilePath.cacheDirectory.relativePath, true)
Task { Task {
await setupBackground() await setup()
} }
return true return true
} }
private nonisolated func setupBackground() async { private func setup() async {
do { do {
try await UIProfileUpdateTask.configure() try await UIProfileUpdateTask.configure()
NSLog("setup background task success") NSLog("setup background task success")