Opening a Realm connection properly and connecting to Atlas

I am following along the tutorial for the Chat message and have some data. I am not sure how to query specific messages:

     let realm = try! Realm()
        let specificPerson = realm.object(ofType: ChatMessage.self, 
forPrimaryKey:  ObjectId("6367f704ebd3cbf5c024c338"))

returns Nil, i am not able to get the specific object. Even when i do

    let chatM = realm.objects(ChatMessage.self)

I get an empty object :

Results<ChatMessage> <0x7fe4d163efa0> (


I figure it has to do with Realm.Configuation.defaultConfiguration ? but i am not sure what the fileURL would be?
Open a Default Realm or Realm at a File URL

I believe this is a cross post to you StackOverflow Question

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:

If you’re querying for a ChatMessage, it will not return a specificPerson but a ChatMessage. Also, that’s not a query. That Finds a specific object by it’s primary key

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.

Thanks for the response Jay!


import RealmSwift

class ChatMessage: Object {
    @Persisted(primaryKey: true) var _id:ObjectId
    @Persisted var room: String
    @Persisted var author: String
    @Persisted var text: String
    @Persisted var timeStamp = Date()
    convenience init(room: String, author: String, text: String){
        self.init() = room = author
        self.text = text


import RealmSwift

struct RoomsView: View {
    let userName: String
    let rooms = ["Java", "Kotlin", "Swift", "JavaScript", "Naruto"]
    var body: some View {
        List {
            if let realmUser = realmApp.currentUser {
                ForEach(rooms, id: \.self){ room in
                    NavigationLink(destination: ChatView(userName: userName, room: room)
                        .environment(\.realmConfiguration, realmUser.configuration(partitionValue: room))){

        .navigationBarTitle("Language", displayMode: .inline)

```import SwiftUI
import RealmSwift

struct ChatView: View {
    @ObservedResults(Chatty.self, sortDescriptor: SortDescriptor(keyPath: "timeStamp", ascending: true)) var messages

    let userName: String
    let room: String

    @State private var messageText = ""
    var body: some View {
        VStack {
            ForEach(messages) { message in
                    if == userName {Spacer()}
                    if != userName {Spacer()}

            HStack {
                TextField("New message", text: $messageText)
                Button(action: fetchData) { Image(systemName: "paperplane.fill") }
                    .disabled(messageText == "")

        .navigationBarTitle("\(room)", displayMode: .inline)
    func fetchData()  {
            let realm = try!  Realm()
            let specificPerson = realm.object(ofType: ChatMessage.self, forPrimaryKey:  ObjectId("6369ee9db15ac444f96eb5d6"))

            let test = realm.objects(ChatMessage.self)


        private func addMessage(){
        let message = Chatty(room: room, author: userName, text: messageText)
        messageText = ""
Console output:

Results<ChatMessage> <0x7fbc58547900> (


Hope this helps clarify my question. I mainly copied the code from mongodb-developer/LiveTutorialChat

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