realm-js
realm-js copied to clipboard
Realm in React Native loses data after long time inactivity and internet disabled
How frequently does the bug occur?
Sometimes
Description
I close the app in the night, and I put my phone on airplane mode to disable the internet. In the morning, I open my app. What is expected: To find my data stored in Realm. What actually happened: All data deleted.
Stacktrace & log output
This how we write data in Realm:
try {
realm.write(() => {
for (let entity in downloadedData) {
const existingData = realm
.objects("MyObject")
.filtered(`id = ${downloadedData[entity].id}`);
if (!existingData.length) {
realm.create(
"MyObject",
{
id: downloadedData[entity].id,
name: downloadedData[entity].name,
},
"all",
);
}
}
});
} catch (error) {
console.error(error);
}
Can you reproduce the bug?
Sometimes
Reproduction Steps
The bug is not systematic. It doesn't happen always and not in all Android devices. To reproduce the bug you should keep your app inactive for a long time and the internet disabled. Then open the app.
Version
SDK 28 & SDK 33
What services are you using?
Local Database only
Are you using encryption?
No
Platform OS and version(s)
Android (both emulator and real device)
Build environment
Which debugger for React Native: .. "react-native": "^0.73.2", "@realm/react": "^0.6.2", "realm": "^12.5.1",
Cocoapods version
1.14.2
Loss of data is of course something we take very seriously, so we're very invested in getting to the bottom of an issue like this.
We do however need a bit more help reproducing this and I have a few questions:
-
keep your app inactive for a long time
How long time are we talking here? Minutes, hours, days? What is the minimum time elapsed where you've ever observed this behaviour?
-
SDK 28 & SDK 33
What component / package are you referring to here?
-
Is it all the data in the realm which gets deleted or just the data that was recently written?
-
Do you have any other code that interact with the database? How are you opening the realm? Etc. I'm curious if any of that code might be causing this.
-
At approximately what frequency do you see this? Let's say you try to reproduce it - how many times (on average) do you need to retry until you see the behaviour?
-
Would you be able to recover and send us the
.realm
file extracted from the device after such an event? -
Are you using any other Realm SDKs (such as native Realm Swift / Realm Java or Kotlin) in the same app?
-
Would you be able to provide an app, with at little code possible which exhibits this behaviour? Either on GitHub or privately via email?
- We are talking about hours (between 10 and 48 hours) The minimum time elapsed is about 10 hours. Sometimes it happens in 15 minutes on Emulator, but not systematic.
- SDK 28 is Android 9 and SDK 33 is Android 13
- All the data in the realm gets deleted.
- Either we delete some data or we delete it all.
Deleting some data:
Deleting all data:try { await realm.write(async () => { if (myData.length) { myData.forEach((data: dataType) => { if (data.attribute) { return; } else { realm.delete(data); } }); } }); } catch (error) { console.log(error) }
const data1: any = useQuery(Class1); const data2 = useQuery(Class2); await new Promise<void>((resolve) => { realm.write(() => { realm.delete(data1); realm.delete(data2); resolve(); }); });
- Sometimes it happens on Emulator in 15 minutes. On Real Device, it happens after 10 hours, but not always and not in all devices. The app has to be closed and no internet.
- We tried to reproduce the bug to send the .realm file, but we didn't succeed. As I said, it's not systematic.
- No, we don't use any other Realm SDKs.
- It may be done but it would take time. If you don't succeed to reproduce the bug, let us know and we will work on it. We hope this information would be helpful.
Can you share the code you use to open the Realm? Additionally, can you set the log level to trace and capture the logs from an app run that results in a loss of data. Finally, have you verified that the Realm file is still available when the app is relaunched after the period of inactivity?
On a side note, the code you posted that deletes from the database is not particularly insightful without context of how it's being used. If you're calling the "delete all data" method every 5 hours, it would explain why your data is gone after a while. You should probably add some log statements around the code you use to delete data and confirm it's not being accidentally called.
The problem is that the bug is not systematic and not in all devices. It happens often in the deployed app in real devices. We will verify the Realm file next time when the bug happens, we added debugging variables in the deployed app. For the code we use to open the Realm, we use only the code above. We have read that Realm.write opens the Realm automatically.
Realm.write opens the Realm automatically.
Realm.write()
handles the transaction but you need to open the Realm elsewhere. We will keep the issue open and wait for you to provide more information once the added debugging variables and logging give results.
I am sharing with you the .realm file extracted from the device after the event (I had to change it to .txt so I can upload it here, you can change the extension after downloading the file) . All the data are reset. As you can see, there is a class called Test. This class we just create it to test, we never delete it in the app, but it's deleted also after such an event. I have to mention that we don't use realm.open at all in our app (it may be a mistake). Also I have to mention that we experienced a scenario where the data is imported in Realm but the .realm file is not created - We imported the data in Realm, we can see it and manage it in the app but when we went to see the .realm file we didn't find it, we closed the app and open it again, realm re-imported the data.
there is a class called Test.
Yes, with no objects.
we don't use realm.open
I suppose, you are using new Realm(config)
. Please post config
.
data is imported in Realm but the .realm file is not created
The Realm file will be created when you open the file. Please use Realm.path
to find the file.
Yes, with no objects.
It has an object which is created after the import of all our data is finished, and we don't delete this object in the app. But it is deleted as you can see.
I suppose, you are using new Realm(config). Please post config.
This is our config:
Schema.ts
const Schema = [Data, Test]; export const realmConfig: Realm.Configuration = { schema: Schema, };
Config.ts
import {createRealmContext} from "@realm/react"; import {realmConfig} from "./Schema";
const realmContext = createRealmContext(realmConfig); export const useRealmContext = () => realmContext;
The Realm file will be created when you open the file. Please use Realm.path to find the file.
The file is not created the 1st time (We checked the file path and we didn't find it), that's why when we closed the app and re-open it, realm imported the data again. But the 2nd time the .realm file was created after the data import is finished (We checked the file path and we found it). So we closed the app and re-open it, realm didn't import the data again.
Hi, I'm a colleague of AbdelhakLabyadh. I have several points to add in the issue:
- First, the issue still persists.
- We updated Realm to 12.7.1, but that changed nothing.
- It also affects iOS devices.
- The issue also appears if we close the app through the OS and then reopen the app. Redux data are still there, but Realm is empty.
Hope this can help.