Skip to content

Commit 532058a

Browse files
authored
fix(datastore): only start the remote sync engine if we have awsapiplugin (#442)
* fix(datastore): only start the remote sync engine if we have awsapiplugin
1 parent 5dc1546 commit 532058a

11 files changed

+41
-12
lines changed

AmplifyPlugins/DataStore/AWSDataStoreCategoryPlugin/AWSDataStorePlugin.swift

+20-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
2323
/// The DataStore configuration
2424
let dataStoreConfiguration: DataStoreConfiguration
2525

26+
let validAPIPluginKey: String
2627
/// The local storage provider. Resolved during configuration phase
2728
var storageEngine: StorageEngineBehavior!
2829

@@ -46,6 +47,7 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
4647
self.modelRegistration = modelRegistration
4748
self.dataStoreConfiguration = dataStoreConfiguration
4849
self.isSyncEnabled = false
50+
self.validAPIPluginKey = "awsAPIPlugin"
4951
if #available(iOS 13.0, *) {
5052
self.dataStorePublisher = DataStorePublisher()
5153
} else {
@@ -57,12 +59,14 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
5759
init(modelRegistration: AmplifyModelRegistration,
5860
configuration dataStoreConfiguration: DataStoreConfiguration = .default,
5961
storageEngine: StorageEngineBehavior,
60-
dataStorePublisher: DataStoreSubscribeBehavior) {
62+
dataStorePublisher: DataStoreSubscribeBehavior,
63+
validAPIPluginKey: String) {
6164
self.modelRegistration = modelRegistration
6265
self.dataStoreConfiguration = dataStoreConfiguration
6366
self.isSyncEnabled = false
6467
self.storageEngine = storageEngine
6568
self.dataStorePublisher = dataStorePublisher
69+
self.validAPIPluginKey = validAPIPluginKey
6670
}
6771

6872
/// By the time this method gets called, DataStore will already have invoked
@@ -78,13 +82,27 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
7882
let filter = HubFilters.forEventName(HubPayload.EventName.Amplify.configured)
7983
var token: UnsubscribeToken?
8084
token = Amplify.Hub.listen(to: .dataStore, isIncluded: filter) { _ in
81-
self.storageEngine.startSync()
85+
if self.hasValidAPIPlugin() {
86+
self.storageEngine.startSync()
87+
} else {
88+
self.log.info("Unable to find suitable plugin for syncEngine. syncEngine will not be started")
89+
}
90+
8291
if let token = token {
8392
Amplify.Hub.removeListener(token)
8493
}
8594
}
8695
}
8796

97+
func hasValidAPIPlugin() -> Bool {
98+
do {
99+
_ = try Amplify.API.getPlugin(for: validAPIPluginKey)
100+
return true
101+
} catch {
102+
return false
103+
}
104+
}
105+
88106
func reinitStorageEngineIfNeeded() {
89107
if storageEngine != nil {
90108
return

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/Sync/APICategoryDependencyTests.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ extension APICategoryDependencyTests {
7676
let dataStorePublisher = DataStorePublisher()
7777
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
7878
storageEngine: storageEngine,
79-
dataStorePublisher: dataStorePublisher)
79+
dataStorePublisher: dataStorePublisher,
80+
validAPIPluginKey: "MockAPICategoryPlugin")
8081
try Amplify.add(plugin: dataStorePlugin)
8182

8283
let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/Sync/LocalSubscriptionTests.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class LocalSubscriptionTests: XCTestCase {
5757
let dataStorePublisher = DataStorePublisher()
5858
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
5959
storageEngine: storageEngine,
60-
dataStorePublisher: dataStorePublisher)
60+
dataStorePublisher: dataStorePublisher,
61+
validAPIPluginKey: "MockAPICategoryPlugin")
6162

6263
let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [
6364
"awsDataStorePlugin": true

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/Sync/MutationQueue/AWSMutationEventIngesterTests.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class AWSMutationEventIngesterTests: XCTestCase {
4747
let publisher = DataStorePublisher()
4848
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
4949
storageEngine: storageEngine,
50-
dataStorePublisher: publisher)
50+
dataStorePublisher: publisher,
51+
validAPIPluginKey: "MockAPICategoryPlugin")
5152

5253
try Amplify.add(plugin: apiPlugin)
5354
try Amplify.add(plugin: dataStorePlugin)

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/Sync/MutationQueue/OutgoingMutationQueueTestsWithMockStateMachine.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ extension OutgoingMutationQueueMockStateTest {
257257
let dataStorePublisher = DataStorePublisher()
258258
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
259259
storageEngine: storageEngine,
260-
dataStorePublisher: dataStorePublisher)
260+
dataStorePublisher: dataStorePublisher,
261+
validAPIPluginKey: "MockAPICategoryPlugin")
261262
try Amplify.add(plugin: dataStorePlugin)
262263
let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [
263264
"awsDataStorePlugin": true

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/Sync/MutationQueue/ProcessMutationErrorFromCloudOperationTests.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,8 @@ extension ProcessMutationErrorFromCloudOperationTests {
847847
let dataStorePublisher = DataStorePublisher()
848848
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
849849
storageEngine: storageEngine,
850-
dataStorePublisher: dataStorePublisher)
850+
dataStorePublisher: dataStorePublisher,
851+
validAPIPluginKey: "MockAPICategoryPlugin")
851852
try Amplify.add(plugin: dataStorePlugin)
852853
let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [
853854
"awsDataStorePlugin": true

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/Sync/MutationQueue/SyncMutationToCloudOperationTests.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ extension SyncMutationToCloudOperationTests {
240240
let dataStorePublisher = DataStorePublisher()
241241
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
242242
storageEngine: storageEngine,
243-
dataStorePublisher: dataStorePublisher)
243+
dataStorePublisher: dataStorePublisher,
244+
validAPIPluginKey: "MockAPICategoryPlugin")
245+
244246
try Amplify.add(plugin: dataStorePlugin)
245247
let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [
246248
"awsDataStorePlugin": true

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/Sync/RemoteSync/RemoteSyncAPIInvocationTests.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ class RemoteSyncAPIInvocationTests: XCTestCase {
5353
let dataStorePublisher = DataStorePublisher()
5454
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
5555
storageEngine: storageEngine,
56-
dataStorePublisher: dataStorePublisher)
56+
dataStorePublisher: dataStorePublisher,
57+
validAPIPluginKey: "MockAPICategoryPlugin")
5758

5859
let apiConfig = APICategoryConfiguration(plugins: [apiPlugin.key: true])
5960
let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [dataStorePlugin.key: true])

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/Sync/SubscriptionSync/ReconcileAndLocalSaveOperationTests.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ extension ReconcileAndLocalSaveOperationTests {
282282
let dataStorePublisher = DataStorePublisher()
283283
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
284284
storageEngine: storageEngine,
285-
dataStorePublisher: dataStorePublisher)
285+
dataStorePublisher: dataStorePublisher,
286+
validAPIPluginKey: "MockAPICategoryPlugin")
286287
try Amplify.add(plugin: dataStorePlugin)
287288
let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [
288289
"awsDataStorePlugin": true

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/TestSupport/BaseDataStoreTests.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ class BaseDataStoreTests: XCTestCase {
4646
let dataStorePublisher = DataStorePublisher()
4747
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
4848
storageEngine: storageEngine,
49-
dataStorePublisher: dataStorePublisher)
49+
dataStorePublisher: dataStorePublisher,
50+
validAPIPluginKey: "MockAPICategoryPlugin")
5051

5152
let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [
5253
"awsDataStorePlugin": true

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/TestSupport/SyncEngineTestBase.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ class SyncEngineTestBase: XCTestCase {
111111
let publisher = DataStorePublisher()
112112
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: modelRegistration,
113113
storageEngine: storageEngine,
114-
dataStorePublisher: publisher)
114+
dataStorePublisher: publisher,
115+
validAPIPluginKey: "MockAPICategoryPlugin")
115116

116117
try Amplify.add(plugin: dataStorePlugin)
117118
}

0 commit comments

Comments
 (0)