Welcome to the forums - in general it’s a good idea to keep questions in one spot so we can focus our attention on it.
The question(s) are still pretty vague - there’s not enough code to understand the issue and the data shown in the question doesn’t match up to the query you’re attempting. Additionally, you’ve got some stuff in the question that I don’t think applies to the question.
The code! Questions need to include a minimum amount of code to understand the issue
We need to know if you’re using a local realm or a synced realm. It appears to be synced. If so, we would need to understand if you’re connecting successfully to Realm as shown in the Getting Started guide Configure and Open a Synched Realm
Then, the naming conventions are a little confusing:
let specificPerson = realm.object(ofType: ChatMessage.self, forPrimaryKey:
Also, that code will not return anything as there is no matching entry - at least by what’s shown in the screenshot
You’re on to something there - but since Realm is a local first database, that tells me that the data on the server doesn’t exist locally. Perhaps you’re attempting to read local data but it’s actually synced? Again, not enough code to understand the use case.
That’s a problem because the link you included indicates you’re trying to use a local only realm, not a synced one.
So - I would suggest editing and clarifying both questions with enough accurate data and code so we can attempt to help.
That’s a bit better but most of that code is unrelated to reading data from Realm so it not necessary.
The issue you’re having is that there is data on the server, but no data stored locally - there’s nothing in your app or code that attempts to work with a Synced Realm - you’re working local only.
For example - the fetchData function connects to a local realm here
let realm = try! Realm()
Says read data from the local realm
I suggest going back to the Getting Started Guides; go through working with a local Realm first. Get your app to where you can read/write and display locally stored data, then once you have a good understanding of that process, then move on to working with a synced Realm as connecting to a synced Realm is entirely different.
For example - as your code shows, that is how you work with a local realm
let realm = try! Realm()
but then how you work with a synced realm is much more verbose as the user needs to be authenrticated first and then create the connection like this
func getRealm() async throws -> Realm {
let user = try await getUser()
let partitionValue = "some partition value"
var configuration = user.configuration(partitionValue: partitionValue)
let realm = try await Realm(configuration: configuration)
return realm
}
let realm = try await getRealm()
// Do something with the realm
So, I suggest learning how to read and write data from a Local Realm and then move onto a Synced Realm
Thanks for the advice Jay. I thought Realm worked by automatically storing data in local and then syncing to Atlas online? This means i just directly added data online before i even stored locally? I followed MongoDb youtube video tutorial of creating first app, i guess this tutorial directly put data online instead of local
That is 100% correct! Realm is a local first database and then synchs in the background. As mentioned previously, while your data may be synced - stored locally and online, the code you’re using is trying to access a completely different, NON Synced Realm aka local-only
For clarity, (conceptually) working with a Synced Realm stores the data in one place on your drive, but if you chose to work with a local-only Realm, data is stored in a different place. They are two different Realms. In fact, the actual Realm files are different for a local-only Realm vs a Synced Realm.
This code
let realm = try! Realm()
is accessing a local-only Realm, which in your case, contains no data. If you want to access a Synched Realm (which is stored in a different place on your drive) you need to follow the code in the guide for working with Synched Realms (links are in my above post).
The file management part is handled by the SDK in both cases so you don’t need to worry about defining file paths etc. But the key is a local-only Realm is completely different than a Synced Realm