From 3c2dd6753fccc75cbe59ddbf62e05b66f551d583 Mon Sep 17 00:00:00 2001 From: John Woo Date: Thu, 12 Nov 2020 16:23:36 -0800 Subject: [PATCH 1/2] fix(DataStore): failed subscriptions lead to instability in sync engine --- .../Reachability/NetworkReachabilityNotifier.swift | 1 + .../OutgoingMutationQueue/MutationRetryNotifier.swift | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/AmplifyPlugins/API/AWSAPICategoryPlugin/Reachability/NetworkReachabilityNotifier.swift b/AmplifyPlugins/API/AWSAPICategoryPlugin/Reachability/NetworkReachabilityNotifier.swift index 4877bb4401..a45bd72766 100644 --- a/AmplifyPlugins/API/AWSAPICategoryPlugin/Reachability/NetworkReachabilityNotifier.swift +++ b/AmplifyPlugins/API/AWSAPICategoryPlugin/Reachability/NetworkReachabilityNotifier.swift @@ -15,6 +15,7 @@ class NetworkReachabilityNotifier { private var reachability: NetworkReachabilityProviding? private var allowsCellularAccess = true + // This is a CurrentValueSubject. Please do not change this unless you talk to wooj2@ or palpatim@ before changing this. let reachabilityPublisher = CurrentValueSubject(ReachabilityUpdate(isOnline: false)) var publisher: AnyPublisher { return reachabilityPublisher.eraseToAnyPublisher() diff --git a/AmplifyPlugins/DataStore/AWSDataStoreCategoryPlugin/Sync/MutationSync/OutgoingMutationQueue/MutationRetryNotifier.swift b/AmplifyPlugins/DataStore/AWSDataStoreCategoryPlugin/Sync/MutationSync/OutgoingMutationQueue/MutationRetryNotifier.swift index 8c124b6afb..721775680d 100644 --- a/AmplifyPlugins/DataStore/AWSDataStoreCategoryPlugin/Sync/MutationSync/OutgoingMutationQueue/MutationRetryNotifier.swift +++ b/AmplifyPlugins/DataStore/AWSDataStoreCategoryPlugin/Sync/MutationSync/OutgoingMutationQueue/MutationRetryNotifier.swift @@ -22,10 +22,10 @@ final class MutationRetryNotifier { retryMutationCallback: @escaping BasicClosure) { self.retryMutationCallback = retryMutationCallback - networkReachabilityPublisher?.subscribe(self) - let deadline = DispatchTime.now() + advice.retryInterval scheduleTimer(at: deadline) + + networkReachabilityPublisher?.dropFirst().subscribe(self) } deinit { From 383ac9f37c68c5f3723432a2a48d5e93c21a022e Mon Sep 17 00:00:00 2001 From: John Woo Date: Thu, 12 Nov 2020 16:53:57 -0800 Subject: [PATCH 2/2] updating publisher to a CurrentValueSubject to mirror the real Reachability Publisher --- .../MutationQueue/SyncMutationToCloudOperationTests.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/Sync/MutationQueue/SyncMutationToCloudOperationTests.swift b/AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/Sync/MutationQueue/SyncMutationToCloudOperationTests.swift index 437f5b3cbb..c4497fc31d 100644 --- a/AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/Sync/MutationQueue/SyncMutationToCloudOperationTests.swift +++ b/AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/Sync/MutationQueue/SyncMutationToCloudOperationTests.swift @@ -20,13 +20,13 @@ class SyncMutationToCloudOperationTests: XCTestCase { let secondsInADay = 60 * 60 * 24 var mockAPIPlugin: MockAPICategoryPlugin! - var reachabilityPublisher: PassthroughSubject! + var reachabilityPublisher: CurrentValueSubject! var publisher: AnyPublisher { return reachabilityPublisher.eraseToAnyPublisher() } override func setUp() { - reachabilityPublisher = PassthroughSubject() + reachabilityPublisher = CurrentValueSubject(ReachabilityUpdate(isOnline: false)) tryOrFail { try setUpWithAPI() }