[英]Unable to sync after Amplify.Datastore.clear()
I have a home page that does the initial datastore sync in the init() function. There are QueryPredicates that are based on a variable that can change (on a different page).我有一个主页,它在 init() function 中执行初始数据存储同步。有一些 QueryPredicates 基于可以更改的变量(在不同的页面上)。
So basically, after I start the app, everything syncs correctly.所以基本上,在我启动应用程序后,一切都会正确同步。 Then, I go to another page and change the value that those QueryPredicates depend on, but the sync doesn't occur.
然后,我 go 到另一个页面并更改那些 QueryPredicates 依赖的值,但同步没有发生。 Instead, I get an error (that I'm almost positive is caused from the datastore.clear()).
相反,我得到一个错误(我几乎肯定是由 datastore.clear() 引起的)。
Here is a look at what the QueryPredicate and value change looks like...下面是 QueryPredicate 和值变化的样子......
List<String> variableThatChanges = ['initialValue']; // belongs to a global class instance
QueryPredicate sampleTest() {
QueryPredicate res = Sample.AUTHGROUP
.eq(variableThatChanges.isEmpty ? '' : variableThatChanges[0]);
return res;
}
// And when the value is changed, it's changed like this...
variableThatChanges = ['newValue'].toList(); // Not sure if .toList() is actually necessary or not
And this is the rest of the logic...这是逻辑的 rest...
// Change the variable value, then...
await Amplify.DataStore.stop();
await Amplify.DataStore.clear();
await Amplify.DataStore.start();
Amplify.DataStore.listen([HubChannel.DataStore], (msg) {// do things with the events});
await Amplify.DataStore.save("basic log message to log the change and initiate sync if hasn't already happened");
NOTE: I actually have these commands wrapped in a try/catch and have some other small things going on so each of the above commands is in it's own function and I call them in the following way...
stopStore().then((_) => clearStore()).then((_) => startStore()).then((_) => listenToHub()).then((_) => logEvent());
The error that is produced is the following:产生的错误如下:
I/amplify:aws-api(11613): No more active subscriptions. Closing web socket.
W/amplify:aws-api(11613): Thread interrupted awaiting subscription acknowledgement.
W/amplify:aws-api(11613): at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedNanos(AbstractQueuedSynchronizer.java:1081)
W/amplify:aws-api(11613): at java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireSharedNanos(AbstractQueuedSynchronizer.java:1369)
W/amplify:aws-api(11613): at java.util.concurrent.CountDownLatch.await(CountDownLatch.java:278)
W/amplify:aws-api(11613): at com.amplifyframework.api.aws.SubscriptionEndpoint$Subscription.awaitSubscriptionReady(SubscriptionEndpoint.java:381)
W/amplify:aws-api(11613): at com.amplifyframework.api.aws.SubscriptionEndpoint.requestSubscription(SubscriptionEndpoint.java:183)
W/amplify:aws-api(11613): at com.amplifyframework.api.aws.MutiAuthSubscriptionOperation.dispatchRequest(MutiAuthSubscriptionOperation.java:113)
W/amplify:aws-api(11613): at com.amplifyframework.api.aws.MutiAuthSubscriptionOperation.$r8$lambda$iziEcYpvlINdYbit2it7fDbbt8A(Unknown Source:0)
W/amplify:aws-api(11613): at com.amplifyframework.api.aws.MutiAuthSubscriptionOperation$$ExternalSyntheticLambda4.run(Unknown Source:2)
W/amplify:aws-api(11613): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:463)
W/amplify:aws-api(11613): at java.util.concurrent.FutureTask.run(FutureTask.java:264)
W/amplify:aws-api(11613): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
W/amplify:aws-api(11613): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
W/amplify:aws-api(11613): at java.lang.Thread.run(Thread.java:1012)
D/TrafficStats(11613): tagSocket(71) with statsTag=0xffffffff, statsUid=-1
E/amplify:aws-datastore(11613): Failure encountered while attempting to start API sync.
...
W/amplify:aws-datastore(11613): API sync failed - transitioning to LOCAL_ONLY.
...
I/amplify:aws-datastore(11613): Orchestrator transitioning from SYNC_VIA_API to LOCAL_ONLY
I/amplify:aws-datastore(11613): Setting currentState to LOCAL_ONLY
I/amplify:aws-datastore(11613): Stopping subscription processor.
Of note, the only other time we use Datastore.clear() in our app is prior to the the user signing out, and the same No more active subscriptions.值得注意的是,我们在我们的应用程序中唯一一次使用 Datastore.clear() 是在用户注销之前,同样没有更多的活动订阅。 Closing web socket.
关闭 web 套接字。 message comes up.
消息出现。
Any help at figuring out how to fix this to allow for the re-sync after changing the QueryPredicate value would really be appreciated.任何帮助找出如何解决此问题以允许在更改 QueryPredicate 值后重新同步的任何帮助都将不胜感激。
It's weird, a lot of times, if I go back to the homepage the init() code with run again and then things start working as desired.这很奇怪,很多时候,如果我 go 回到主页,init() 代码再次运行,然后事情开始按预期工作。 This isn't all the time, but most of the time.
这不是所有时间,而是大多数时间。 The thing about that is, that I believe Amplify.configure() is being called again (which I know isn't recommended to do more than once in the apps lifecycle).
事情是,我相信 Amplify.configure() 被再次调用(我知道不建议在应用程序生命周期中多次调用)。
I've tried everything listed above, but can't get the subscription error to go away and the app to re-sync correctly.我已经尝试了上面列出的所有内容,但无法消除 go 的订阅错误,也无法正确重新同步应用程序。
The end goal is to be able to clear the datastore and get a fresh sync using the new variable in the QueryPredicate.最终目标是能够清除数据存储并使用 QueryPredicate 中的新变量获得新的同步。
After reviewing open tickets in the amplify-flutter
github repo I came across this one:在查看
amplify-flutter
github 回购中的公开票证后,我发现了这个:
https://github.com/aws-amplify/amplify-flutter/issues/1479 https://github.com/aws-amplify/amplify-flutter/issues/1479
Basically, you can't await Datastore.clear()
or Datastore.close()
(on Android).基本上,您不能
await Datastore.clear()
或Datastore.close()
(在 Android 上)。 It's a bug that is supposed to be being worked on, but the ticket is still open and it'll be a year next month, so who knows when it'll be fixed.这是一个应该正在处理的错误,但票证仍然开放,下个月将是一年,所以谁知道什么时候会被修复。
Anyways, one of the suggestions in there is to simply implement a manual delay of 2 seconds after you call Datastore.clear()
.无论如何,其中的一个建议是在调用
Datastore.clear()
后简单地实现 2 秒的手动延迟。
I ended up doing this after both of my calls, ( Datastore.stop()
and Datastore.clear()
) and everything works just fine now.在我的两个电话(
Datastore.stop()
和Datastore.clear()
)之后,我最终都这样做了,现在一切正常。 It's just a timing issue with the asynchronous functions.这只是异步函数的时间问题。
So, as mentioned, this isn't the best solution, but until the amplify-flutter team implements a fix, this seems to be a valid workaround.因此,如前所述,这不是最佳解决方案,但在 amplify-flutter 团队实施修复之前,这似乎是一个有效的解决方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.