David Carney
David Carney

Reputation: 1

MongoDB Realm cannot update user embedded object in Swift

I'm trying to add to an embedded "Conversation" object within my user collection (for a chat app) in Mongo Realm. I would like to create a "default" conversation when the user account is created, such that every user should be a member of at least one conversation that they can then add others to.

The app currently updates the user collection via a trigger and function in Realm at the back end using the email / Password authentication process.

My classes are defined in Swift as follows:

@objcMembers class User: Object, ObjectKeyIdentifiable {
    dynamic var _id = UUID().uuidString
    dynamic var partition = "" // "user=_id"
    dynamic var userName = ""
    dynamic var userPreferences: UserPreferences?
    dynamic var lastSeenAt: Date?
    var teams = List<Team>()
    dynamic var presence = "Off-Line"

    var isProfileSet: Bool { !(userPreferences?.isEmpty ?? true) }
    var presenceState: Presence {
        get { return Presence(rawValue: presence) ?? .hidden }
        set { presence = newValue.asString }
    }

    override static func primaryKey() -> String? {
        return "_id"
    }

@objcMembers class Conversation: EmbeddedObject, ObjectKeyIdentifiable {
    dynamic var id = UUID().uuidString
    dynamic var displayName = ""
    dynamic var unreadCount = 0
    var members = List<Member>()
    
}

So my current thinking is that I should code it in Swift as follows which I believe should update the logged in user, but sadly can't get this quite right:

 // Open the default realm
        let realm = try! Realm()
        
        try! realm.write {
            let conversation = Conversation()
            conversation.displayName = "My Conversation"
            conversation.unreadCount = 0
        
            var user = app.currentUser
            let userID = user.id
            
            let thisUser = User(_id: userID)
            realm.add(user)
        }

Can anyone please spot where I'm going wrong in my code?

Hours spent on Google and I'm missing something really obvious! I have a fair bit of .NET experience and SQL but struggling to convert to the new world!

I'm a noob when it comes to NoSQL databases and SwiftUI and trying to find my way looking at a lot of Google examples. My example us based on the tutorial by Andrew Morgan https://developer.mongodb.com/how-to/building-a-mobile-chat-app-using-realm-new-way/

Upvotes: 0

Views: 479

Answers (1)

Jay
Jay

Reputation: 35648

I am a bit unclear on the exact use case here but I think what's being asked is how to initialize an object with default values - in this case, a default conversation, which is an embedded object.

If that's not the question, let me know so I can update.

Starting with User object

class UserClass: Object {
    @objc dynamic var _id = ObjectId.generate()
    @objc dynamic var name = ""

    let conversationList = List<ConversationClass>()

    convenience init(name: String) {
        self.init()
        self.name = name

        let convo = ConversationClass()
        self.conversationList.append(convo)
    }

    override static func primaryKey() -> String? {
        return "_id"
    }
}

and the EmbeddedObject ConversationClass

class ConversationClass: EmbeddedObject {
    dynamic var displayName = "My Conversation"
    dynamic var unreadCount = 0
    var members = List<MemberClass>()
}

The objective is that when a new user is created, they have a default conversation class added to the conversationList. That's done in the convenience init

So the entire process is like this:

let realm = Realm()
let aUser = UserClass(name: "Leroy")
try! realm.write { 
   realm.add(aUser)
}

Will initialize a new user, set their name to Leroy. It will also initialize a ConversationClass Embedded object and add it to the conversationList.

The ConversationClass object has default values set for displayName and unread count per the question.

Upvotes: 0

Related Questions