The network connection was lost
PatilAkshays opened this issue · 11 comments
Describe the bug
I am using amplify AWS to sync Application with cloud, some times I am getting error as "The network connection was lost" I have strong internet connection but it is throwing this error.
To Reproduce
Steps to reproduce the behavior:
- Not able to reproduce, it is causing for some particular users only.
@PatilAkshays Thanks for submitting the issue. Can you provide additional details to help reproduce the issue? Which library and version are you using? Is there a specific API that is getting the network connection lost? Can you provide a sample app that reproduces the issue or additional app logs?
Code Snippet
private func configureAmplify() {
do {
if AppScheme.current.allowsDebugLogging {
SDKLoggingSystem.initialize(logLevel: .debug)
Amplify.Logging.logLevel = .debug
}
try Amplify.add(plugin: AWSCognitoAuthPlugin())
try Amplify.add(plugin: AWSAPIPlugin(sessionFactory: self))
try DataStoreManager.configure(onError: { [weak self] error in
Snacker.debugSnack(.error(error.amplifyDescriptionOrDefault))
self?.eventTracker.track(
event: .error(
name: "DataStoreError",
error: error,
message: "❌ DataStore Synchronization error: \(error.amplifyDescriptionOrDefault)"
),
extras: [
"error": error.amplifyDescriptionOrDefault
]
)
})
try Amplify.add(plugin: AWSS3StoragePlugin())
configureDataStoreEventListener()
try Amplify.configure()
eventTracker.track(event: .logOnly(message: "AWS Amplify configured"))
} catch {
Snacker.debugSnack(.error(error.amplifyDescriptionOrDefault))
eventTracker.track(
event: .error(
name: "AmplifyConfigurationError",
error: error,
message: "Failed to configure AWS Amplify: \(error.amplifyDescriptionOrDefault)"
),
extras: [
"error": error.amplifyDescriptionOrDefault
]
)
}
}
public static func configure(onError: @escaping (Error) -> Void = { _ in }) throws {
let syncExpressions = [
DataStoreSyncExpression.syncExpression(XYZTableModel.schema, where: {
if let dateLimit = dateLimit {
XYZTableModel.keys.user_id.eq(userId).and(XYZTableModel.keys.data_timestamp.gt(dateLimit.temporalDateTime))
} else {
XYZTableModel.keys.user_id.eq(userId)
}
}),
]
try Amplify.add(plugin: AWSDataStorePlugin(
modelRegistration: DataStoreModels(),
configuration: .custom(errorHandler: onError, syncMaxRecords: UInt.max, syncExpressions: syncExpressions)
))
}
To Reproduce
Steps to reproduce the behavior:
Synching data with Cloud while application is in background mode using REST api in for loop.
Lock the phone
Wait few seconds
Unlock the phone
Move the app to foreground
Check the debug console
Observed Behavior
Data is not synced with cloud, throwing "network connection was lost." error while we have strong internet connection.
This not happening continuously, middle of operation it will throw the error.
Expected Behavior
It should upload the data on clould.
Environment(please complete the following information):
SDK Version:
.package(url: "https://github.com/aws-amplify/amplify-ios", branch: "main")
Dependency Manager: Package Dependancies
Swift Version : 5
Xcode Version: 15.0.1 (15A507)
Device Information (please complete the following information):
Device: iPhone
iOS Version: 17
Logs:
2023/12/13 05:17:41:664 Network reachability changed: satisfied
2023/12/13 05:17:41:811 Network reachability changed: satisfied
2023/12/13 05:17:41:852 ❌ DataStore Synchronization error: [DataStoreError] Api - Error: [APIError] NetworkError error. desc: The network connection was lost., userinfo: nil, The network connection was lost., event: nil
2023/12/13 05:17:41:867 ❌ DataStore Synchronization error: [DataStoreError] Api - Error: [APIError] NetworkError error. desc: The network connection was lost., userinfo: nil, The network connection was lost., event: nil
2023/12/13 05:17:41:871 ❌ DataStore Synchronization error: [DataStoreError] Api - Error: [APIError] NetworkError error. desc: The network connection was lost., userinfo: nil, The network connection was lost., event: nil
2023/12/13 05:17:41:873 ❌ DataStore Synchronization error: [DataStoreError] Api - Error: [APIError] NetworkError error. desc: The network connection was lost., userinfo: nil, The network connection was lost., event: nil
2023/12/13 05:17:41:876 ❌ DataStore Synchronization error: [DataStoreError] Api - Error: [APIError] NetworkError error. desc: The network connection was lost., userinfo: nil, The network connection was lost., event: nil
Thanks for the details @PatilAkshays
Synching data with Cloud while application is in background mode using REST api in for loop.
Can you clarify "REST api in for loop."? From the code snippet i looks like you are using DataStore Amplify.DataStore.save()
and the AppSync conflict resolution enabled backend, but want to confirm since REST APIs usually refer to the Amplify.API.post()
API calls and a provisioned a REST API in APIGateway.
If the app was moved to the background, there are certain restrictions that the OS will impose on the network activity of the app. The failures are expected and when the app goes back to the foreground and DataStore is active, then the failed mutation events should be retried.
I misunderstood the logic, I have verified the concept and added new scenario.
Steps to reproduce the behavior:
Synching data with Cloud.
try await Amplify.DataStore.start()
(Calling while sync the data store with cloud)
Wait few seconds
Check the debug console
Observed Behavior
Data is not synced with cloud, throwing
The network connection was lost.
error while we have strong internet connection.
This not happening continuously, middle of operation it will throw the error.
try Amplify.add(plugin: AWSDataStorePlugin(
modelRegistration: DataStoreModels(),
configuration: .custom(errorHandler: onError, syncMaxRecords: UInt.max, syncExpressions: syncExpressions)
))
we are getting error on above plugin error handler.
Expected Behavior
Data Sync should be happened.
Application Behavior
Syncing start
Saving data with each stores (abcStore, xyzRateStore, ...etc)
Once all data gets saved using each store at end call Amplify.DataStore.Start()
Can you please clarify on these below points
- Does
DataStore.Start()
try to sync all data store models and due to load it throws the error as "The network connection was lost." - If
DataStore.Start()
is already InProgress, hitting it again and again. Does it needDataStore.Stop()
and thenDataStore.Start()
Hi @PatilAkshays, any DataStore operation performed such as Amplify.DataStore.save()
will implicitly start DataStore (same as DataStore.start()). Because of this implicit start, the logs will get very noisy, with the sync engine performing synchronization and reconciliation, subscription establishments, and syncing the data to the cloud. One way to isolate this is to
- Call
DataStore.clear()
to ensure everything has stopped and local database is deleted. - Call
DataStore.start()
, and make sure that DataStore emits aready
event. - Clear the logs and call
DataStore.save()
, to observe the sync to cloud logs
You can see the ready
event from the DataStore hub channel:
Amplify.Hub.publisher(for: .dataStore).sink { event in
print("[DataStore] \(event)")
}.store(in: &sinks)
Moving out the implicit start from the save will make it easier to pinpoint where the error is coming from, ie. is the network connection error coming from the subscriptions or the mutation request for syncing to cloud, or somewhere else after DataStore.start is called?
Calling DataStore.start() on an already started DataStore will do nothing and return. You can stop and start it to restart the sync process, but usually this is done in other use cases like changing the syncExpression and restarting to sync to use the new filter
@lawmicha we're still seeing this issue as well for multiple users. We'll post whatever logs we have. Considering that we're relying on data in DynamoDB for our algorithms, this is a must-fix. Has any progress been made or you're looking for more information?
@madej10 Thank you for your message. Could you please open a new issue with your environment information, verbose logs and reproduction steps? This will help in tracking it better.
**
Real-time event subscriptions do not working when the app is in the background.
**
real-time event subscriptions do not working when the app is in the background.
Issue Description: When subscribing to real-time events using the Amplify API, the callback functions work fine when the app is in the foreground. However, once the app is moved to the background, the callbacks are not triggered. I have also enabled background mode from Xcode capabilities, but the issue persists.
Amplify Version: -- 12.12.2
Sample Code: {
func createSubscription() {
subscription = Amplify.API.subscribe(request: .subscription(of: Todo.self, type: .onCreate))
Task {
do {
if let subscriptions = subscription {
for try await subscriptionEvent in subscriptions {
switch subscriptionEvent {
case .connection(let subscriptionConnectionState):
print("Subscription connect state is \(subscriptionConnectionState)")
case .data(let result):
switch result {
case .success(let createdTodo):
isOnCreateSubscriptionEstablished = true
print("Successfully got todo from subscription: \(createdTodo)")
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
}
}
}
} catch {
print("Subscription has terminated with \(error)")
}
}
}
}
}
This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.