Realm Data Doesn't Show Up on a Physical Device

Why Realm doesn't find object which I inserted in transaction before?

But when I actually faced the Realm, it turned out to be really painful experience for me to implement caching logic with it.

Reading the docs regarding best practices help. For example, the default idea is that you define a RealmResults using an async query on the UI thread, add a change listener to it, and observe the latest emission of the database.

There is no "caching" involved in that beyond saving to the database and observing the database. Any additional complexity is added by you and is completely optional.

All problems come from applying transaction to database which then must be synced on all threads working with Realm.

All looper threads automatically make the Realm auto-refresh, therefore if addChangeListener is used as intended in the docs, then there is no need for trickery, Realm will manage the synchronization between threads.

I want to save my object to Realm and then query it out of.

realm.executeTransactionAsync({

No reason to use executeTransactionAsync when you are already on a background thread.

try(Realm realm = Realm.getDefaultInstance()) {
realm.executeTransaction((r) -> {
// do write here
});
}
realm.where(RealmChat::class.java)

If you do import io.realm.kotlin.where, then you can do realm.where<RealmChat>().

.findFirstAsync()

No reason to use findFirstAsync() instead of findFirst() when you are already on a background thread. Also no reason to use load() when you're on a background thread, because you should be using findFirst() in the first place anyway.


You are also most likely missing a return@defer Single.just(chat) to actually return the chat if it's found. That is most likely what your original problem is.




With the handler thread things you're doing though, you might want to consider taking a look at this project called "Monarchy", as it intends to set up the ability to run queries on a background looper thread while still observing the results. It is labelled stagnant but the ideas are sound.

Cannot write to my prebundled realm file

writeCopy(toFile:) is not an alternative to using write. It actually copies your existing .realm file and copies it to the URL supplied as the input argument.

You need to call a regular write transaction if you want to modify your .realm file and create a copy of the .realm before modifying it if you want to store several versions.

However, you'll run into troubles soon if you are modifying bundled files since that will break the code signature of your application. If you want to use a prepopulated .realm file from the application Bundle, first copy it to the default location used by realm, then you can simply write to the default realm. If you don't know how to copy a prepopulated Realm file to the default location, have a look at this answer of mine.

Why can I no longer open my realm after upgrading my realm version

TL;DR

In a nutshell, the issue is that Realm made significant changes to the underlying data structure from older versions to newer versions. While it does not require a migration* (the objects schema doesn't change) it does require that the SDK is updated at the same time the backing data is. This also requires Realm Studio to be updated accordingly.

MORE INFO

Here's where the issue is

I've been using Realm 3.17.3 in my swift app.

and then

updated my podfile to use Realm 10.7.2.

From the Realm 10.0 Version Docs

NOTE: This version upgrades the Realm file format version to add
support for new data types. Realm files opened will be automatically
upgraded and cannot be read by versions older than v10.0.0.

What that's saying is the data that backs your existing Realm objects is totally changed. So, for example, opening your Realm file with Realm Studio (10+) will update all of your data (it does show a message) and then your existing app will no longer be able to read it and will crash with this error

Invalid top array (ref: 53728, size: 11) Path:

Also note that if you do the inverse, where the SDK is updated and you try to open Realm with Realm Studio and get that error, you're using an older version of Realm Studio. You will need to update to Realm Studio 10.0 or higher.

Note that by updating to 10.0 or higher, that version can no longer open the legacy Realm Cloud or Realm Object Server. You need to keep version 5.x around for that.

*If you're using Realm Sync/MongoDB Realm Sync, there are significant changes from prior versions. Please review the MongoDB Realm Sync docs.

Realm app compiles on device but not on simulator on Xcode 12

In my case upgrading Realm to the latest version, 10.1.0 and updating cocoapods to 1.10.0 solved the issue, I can now compile on the Simulator.

Realm Migration doesn't work

As long as you're in local development only, I'd recommend to reset your Realm database instead of doing a migration. Migrations are the way to go, if you have already shipped a version of your app with another schema and want to keep user data.

You can delete the database by deleting the app from the simulator or the device.
Alternatively you can use NSFileManager to delete the Realm file before accessing the database.

let defaultPath = Realm.Configuration.defaultConfiguration.path!
try NSFileManager.defaultManager().removeItemAtPath(defaultPath)


Related Topics



Leave a reply



Submit