fix: ios skip posting hash response after detached from engine (#22695)

* skip posting message after detached from engine

* review changes

* cancel plugin before destroying engine

---------

Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
Co-authored-by: Alex <alex.tran1502@gmail.com>
This commit is contained in:
shenlong 2025-10-14 07:39:12 +05:30 committed by GitHub
parent 46869f664d
commit cf52b879b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 62 additions and 15 deletions

View file

@ -17,13 +17,25 @@ struct AssetWrapper: Hashable, Equatable {
}
}
class NativeSyncApiImpl: NativeSyncApi {
class NativeSyncApiImpl: ImmichPlugin, NativeSyncApi, FlutterPlugin {
static let name = "NativeSyncApi"
static func register(with registrar: any FlutterPluginRegistrar) {
let instance = NativeSyncApiImpl()
NativeSyncApiSetup.setUp(binaryMessenger: registrar.messenger(), api: instance)
registrar.publish(instance)
}
func detachFromEngine(for registrar: any FlutterPluginRegistrar) {
super.detachFromEngine()
}
private let defaults: UserDefaults
private let changeTokenKey = "immich:changeToken"
private let albumTypes: [PHAssetCollectionType] = [.album, .smartAlbum]
private let recoveredAlbumSubType = 1000000219
private var hashTask: Task<Void, Error>?
private var hashTask: Task<Void?, Error>?
private static let hashCancelledCode = "HASH_CANCELLED"
private static let hashCancelled = Result<[HashResult], Error>.failure(PigeonError(code: hashCancelledCode, message: "Hashing cancelled", details: nil))
@ -272,7 +284,7 @@ class NativeSyncApiImpl: NativeSyncApi {
}
if Task.isCancelled {
return completion(Self.hashCancelled)
return self?.completeWhenActive(for: completion, with: Self.hashCancelled)
}
await withTaskGroup(of: HashResult?.self) { taskGroup in
@ -280,7 +292,7 @@ class NativeSyncApiImpl: NativeSyncApi {
results.reserveCapacity(assets.count)
for asset in assets {
if Task.isCancelled {
return completion(Self.hashCancelled)
return self?.completeWhenActive(for: completion, with: Self.hashCancelled)
}
taskGroup.addTask {
guard let self = self else { return nil }
@ -290,7 +302,7 @@ class NativeSyncApiImpl: NativeSyncApi {
for await result in taskGroup {
guard let result = result else {
return completion(Self.hashCancelled)
return self?.completeWhenActive(for: completion, with: Self.hashCancelled)
}
results.append(result)
}
@ -299,7 +311,7 @@ class NativeSyncApiImpl: NativeSyncApi {
results.append(HashResult(assetId: missing, error: "Asset not found in library", hash: nil))
}
completion(.success(results))
return self?.completeWhenActive(for: completion, with: .success(results))
}
}
}