From 58bc9baa0509bffd211844c6adfe10420ad7ba01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Fri, 22 Sep 2023 18:00:19 +0800 Subject: [PATCH] Fix profile upate task --- .../Service/UIProfileUpdateTask.swift | 19 ++++++++++++------- .../Views/Profile/EditProfileView.swift | 10 +++++----- .../Views/Profile/NewProfileView.swift | 7 +++++++ SFI/ApplicationDelegate.swift | 14 +++++++------- SFT/ApplicationDelegate.swift | 4 ++-- 5 files changed, 33 insertions(+), 21 deletions(-) diff --git a/ApplicationLibrary/Service/UIProfileUpdateTask.swift b/ApplicationLibrary/Service/UIProfileUpdateTask.swift index 1ec272c..07da12a 100644 --- a/ApplicationLibrary/Service/UIProfileUpdateTask.swift +++ b/ApplicationLibrary/Service/UIProfileUpdateTask.swift @@ -5,16 +5,21 @@ import Library #if os(iOS) || os(tvOS) public class UIProfileUpdateTask: BGAppRefreshTask { private static let taskSchedulerPermittedIdentifier = "\(FilePath.packageName).update_profiles" + + private static var registered = false public static func configure() async throws { - let success = BGTaskScheduler.shared.register(forTaskWithIdentifier: taskSchedulerPermittedIdentifier, using: nil) { task in - NSLog("profile update task started") - Task { - await getAndupdateProfiles(task) + if !registered { + let success = BGTaskScheduler.shared.register(forTaskWithIdentifier: taskSchedulerPermittedIdentifier, using: nil) { task in + NSLog("profile update task started") + Task { + await getAndupdateProfiles(task) + } } - } - if !success { - throw NSError(domain: "register failed", code: 0) + if !success { + throw NSError(domain: "register failed", code: 0) + } + registered = true } BGTaskScheduler.shared.cancelAllTaskRequests() let profiles = try await ProfileManager.listAutoUpdateEnabled() diff --git a/ApplicationLibrary/Views/Profile/EditProfileView.swift b/ApplicationLibrary/Views/Profile/EditProfileView.swift index 5930967..4a12e25 100644 --- a/ApplicationLibrary/Views/Profile/EditProfileView.swift +++ b/ApplicationLibrary/Views/Profile/EditProfileView.swift @@ -170,11 +170,6 @@ public struct EditProfileView: View { do { try await Task.sleep(nanoseconds: UInt64(100 * Double(NSEC_PER_MSEC))) try await profile.updateRemoteProfile() - #if os(iOS) || os(tvOS) - try await UIProfileUpdateTask.configure() - #else - try await ProfileUpdateTask.configure() - #endif } catch { alert = Alert(error) } @@ -194,6 +189,11 @@ public struct EditProfileView: View { private func saveProfile() async { do { _ = try await ProfileManager.update(profile) +#if os(iOS) || os(tvOS) + try await UIProfileUpdateTask.configure() +#else + try await ProfileUpdateTask.configure() +#endif } catch { alert = Alert(error) return diff --git a/ApplicationLibrary/Views/Profile/NewProfileView.swift b/ApplicationLibrary/Views/Profile/NewProfileView.swift index 71d2823..5476c6c 100644 --- a/ApplicationLibrary/Views/Profile/NewProfileView.swift +++ b/ApplicationLibrary/Views/Profile/NewProfileView.swift @@ -247,5 +247,12 @@ public struct NewProfileView: View { autoUpdateInterval: autoUpdateInterval, lastUpdated: lastUpdated )) + if profileType == .remote { +#if os(iOS) || os(tvOS) + try await UIProfileUpdateTask.configure() +#else + try await ProfileUpdateTask.configure() +#endif + } } } diff --git a/SFI/ApplicationDelegate.swift b/SFI/ApplicationDelegate.swift index 3f6d7ab..21f0dbe 100644 --- a/SFI/ApplicationDelegate.swift +++ b/SFI/ApplicationDelegate.swift @@ -18,19 +18,19 @@ class ApplicationDelegate: NSObject, UIApplicationDelegate { } private func setup() async { - await setupBackground() - if UIDevice.current.userInterfaceIdiom == .phone { - await requestNetworkPermission() - } - } - - private nonisolated func setupBackground() async { do { try await UIProfileUpdateTask.configure() NSLog("setup background task success") } catch { 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, *) { do { let profileServer = try ProfileServer() diff --git a/SFT/ApplicationDelegate.swift b/SFT/ApplicationDelegate.swift index fd8bc6f..9fd4cd8 100644 --- a/SFT/ApplicationDelegate.swift +++ b/SFT/ApplicationDelegate.swift @@ -9,12 +9,12 @@ class ApplicationDelegate: NSObject, UIApplicationDelegate { NSLog("Here I stand") LibboxSetup(FilePath.sharedDirectory.relativePath, FilePath.workingDirectory.relativePath, FilePath.cacheDirectory.relativePath, true) Task { - await setupBackground() + await setup() } return true } - private nonisolated func setupBackground() async { + private func setup() async { do { try await UIProfileUpdateTask.configure() NSLog("setup background task success")