Hello,
It’s more than a year that I have this problem, could anyone please help to solve it? It’s very annoying to have to write something like “Tap the screen if it stuck” in the first view of my app.
opened 08:52AM - 28 Feb 20 UTC
closed 01:35PM - 25 Jun 20 UTC
O-Community
When my app start I show a spinner until the synchronization of my query-synched… realm completes.
## Goals
When the sync ends the spinner disappears because of the promise resolved.
## Expected Results
The promise is resolved and the code go on.
## Actual Results
The problem is that since the promises are not resolved until I tap the screen the loading process seems infinite and the spinner doesn't disappear.
## Steps to Reproduce
I have this problem since almost 1 year. You can reproduce it by:
- use create-react-native-app
- install realm
- try
## Code Sample
```
var realm = Database.getRealm()
var class1 = realm.objects("Class1")
var class2 = realm.objects("Class2")
var class3 = realm.objects("Class3")
await Promise.all(
[
Database.susbscribeAndSyncTo(class1),
Database.susbscribeAndSyncTo(class2),
Database.susbscribeAndSyncTo(class3),
]
)
// This console.log is not executed until I don't tap on the screen.
// More classes I add to this and more times I need to tap the screen
console.log("Synched")
return true
```
```
static susbscribeAndSyncTo = async (object, object_name) => {
var subscription = object.subscribe()
return new Promise((resolve, reject) => {
subscription.addListener((subscription, state) => {
if (this.checkSubscriptionState(state, object_name)) {
try {
subscription.removeAllListeners()
} catch (e) {
console.log(e.message)
}
resolve(true);
}
})
});
}
static checkSubscriptionState = (state, object_type) => {
switch (state) {
case Realm.Sync.SubscriptionState.Creating:
// The subscription has not yet been written to the Realm
break;
case Realm.Sync.SubscriptionState.Pending:
// The subscription has been written to the Realm and is waiting
// to be processed by the server
break;
case Realm.Sync.SubscriptionState.Complete:
// The subscription has been processed by the server and all objects
// matching the query are in the local Realm
return true
break;
case Realm.Sync.SubscriptionState.Invalidated:
// The subscription has been removed
break;
case Realm.Sync.SubscriptionState.Error:
break;
default:
break;
}
return false
}
```
## Version of Realm and Tooling
- Realm JS SDK Version: Realm JS from 3.4.2
- Node or React Native: React Native
- Client OS & Version: Android / iOS
- Which debugger for React Native: None
Mo_Basm
(Mo)
March 27, 2020, 10:13pm
2
We have the exact same problem
I’ve talked with the team from Realm. They say this kind of issues depends from something that is not easy to change in how realm J’s works, so their advice is to use a setTimeout as workaround.
In my example you should write it (in my case with 30ms of delay) after
resolve()
2 Likes
Mo_Basm
(Mo)
March 28, 2020, 11:58pm
4
Oh I see. Thank you for providing a workaround . Will try that out. Cheers.
1 Like