-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GraphQL: Backgrounding iOS App breaks Update and Query #2599
Comments
Hi @skim037 - Can you share your app's build method and the calls to |
I simplified my code as below. void _init() async {
try {
await _configureAmplify();
_waitForDataStore();
setState(() => _isAmplifyConfigured = true);
} on Exception catch (e) {
setState(() => _isAmplifyConfigureFailed = true);
}
}
Future<void> _configureAmplify() async {
try {
await Amplify.addPlugins([
AmplifyAPI(),
AmplifyAuthCognito(),
AmplifyDataStore(
modelProvider: ModelProvider.instance,
),
]);
await Amplify.configure(amplifyconfig);
} on Exception catch (e) {
rethrow;
}
}
void _waitForDataStore() async {
await Amplify.DataStore.stop();
await Amplify.DataStore.clear();
await Amplify.DataStore.start();
StreamSubscription dsHub =
Amplify.Hub.listen([HubChannel.DataStore], (hubEvent) {
});
dsHub.onData((data) async {
if (data.eventName == 'ready') {
await dsHub.cancel();
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: appName,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Container()
);
} I think the issue is raised when using DataStore stop, clear, and start. |
I did more testing. When I changed the default authorization mode to Cognito User Pool, backend sync and query is not working as expected after I put the app in the background for 5 minutes. (When using API Key as a default auth mode, things work as expected). Set upSet default auth mode to Cognito User Pool
Model auth changechanged a model auth to something like this.
Configure multiAuth strategy
Reproduction Steps
Code used.
Behavior I observed
When I use API Key as a default auth mode and use public auth in the models, I don't see these behaviors. Save and query both seem to work when the app was moved to foreground after 5 minutes in the background. Please take a look and let me know what's going on here. I've been trying for days to figure out why my app gets stuck when it goes to background and this is as much details as I can find myself. |
Additional testWhen I use |
Hi @skim037 sorry to hear you're having this issue. This is my understanding of your problem:
Can you please clarify:
OR
If possible, it would be helpful if you could provide all logs when running your debug test so we can better understand your use case here. |
It seems to happen on iOS simulator as well. When the app is moved to background, this log shows up.
After 5min, when the app is moved to foreground, this log shows up.
When tried to save a Todo item, this log shows up
It shows my log When I try to query a Todo record, the request gets stuck at ImplementationThese implementations work before app was moved to background for 5 minutes.
|
Hi @skim037 I haven't been able to replicate your issue on an iOS simulator or physical iphone. Setup: I used your Todo model with same auth rules except for iam. I used authenticator and setup auth default as API Key and Cognito User Pools. Datastore is setup as multiauth. Testing: I call query Todo and call save Todo using a hardcoded id (so the same Todo is saved to each time). When I put the app in background for 5 minutes, I get some errors but it recovers:
I call query and save Todo freely and nothing hangs and the new Todo is synced and saved to cloud. Thoughts: Are you using the Authenticator plugin to manage your user's sign in flow ? Perhaps it could be because iam is configured as well so I can add that later and see if it's still working. |
Hi @fjnoyp |
Hi @fjnoyp I had many other models in my app so I tried removing all models and tested with just the
|
Hi @skim037 thanks for the extra information here. So it sounds like you also don't get the issue when just using one model like I did. I can update my schema.graphql to use all the models you provided here and see if I get the error. |
Hi @skim037 I am still unable to reproduce on simulator or iPhone device. I added iam auth. I also added all of the models defined in your schema.graphql file provided above. I test via two simple query and save methods:
I do not make the following calls like you did though. Why did you add them beforehand?
There is a previous issue in which calling these methods can disrupt Datastore: #1479 Furthermore, while you call When/how do you call save/query when brining the app to foreground afterwards? |
Hi @fjnoyp I'm not sure why you are not experiencing the same issue. I attached the full log from Xcode when running the app on iPhone device. 2023-02-07 20:08:10.468409: First todo write when the app is started. It was successful.
Save operation
This issue seems pretty related. |
@skim037 I was able to reproduce this. In my example, I was able to mitigate the issue by stopping/starting DataStore when backgrounding/foregrounding the app with the WidgetsBindingObserver API and code like this: @override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
print('resumed');
Amplify.DataStore.start();
break;
case AppLifecycleState.inactive:
case AppLifecycleState.paused:
case AppLifecycleState.detached:
print("app in bg");
Amplify.DataStore.stop();
break;
}
} However, this workaround is generally not required so I'm going to do a little more investigation to see if there might be another issue here. |
Also noting that I tried updating amplify-flutter to use 1.29.0 of amplify-swift in v1 track which has a fix for datastore reconnect but it did not seem to fix the issue. |
As amplify-swift states: DataStore doesn't work in iOS background mode. In flight HTTP requests that cannot finish within 5 seconds or so when app entered background will be terminated which causes the exceptions. Hence, this issue should not be a bug, a feature request is more appropriate. |
Description
I created a sample Todo Flutter app and initialized following Amplify plugins.
When I run the app on physical iPhone device, and move the app to the background and wait for ~30s and bring the app back to the foreground, I see these messages in debug log.
I was able to query and save the data to the backend but I wanted to check if this is ignorable or a real issue. I don't see these messages when running on simulator or run the app without Amplify plugins.
I added more models to
schema.graphql
, and did the same test(move to background, wait ~30s and bring back to foreground). I see these errors.What is going on here and what's causing all these error? I'm not doing anything in my app. I just added Amplify plugin and that's it.
Categories
Steps to Reproduce
No response
Screenshots
No response
Platforms
Android Device/Emulator API Level
No response
Environment
Dependencies
Device
iPhone 12
OS
iOS 16.2
Deployment Method
Amplify CLI
CLI Version
10.6.2
Additional Context
No response
Amplify Config
{
"UserAgent": "aws-amplify-cli/2.0",
"Version": "1.0",
}
The text was updated successfully, but these errors were encountered: