Realm objects returns nil after adding



I created an Person object when the User logs in:

let creds = SyncCredentials.jwt(accessToken)
SyncUser.logIn(with: creds, server: Constants.syncAuthURL, onCompletion: { [weak self](user, err) in
    if let user = user {
        self?.setDefaultRealmConfiguration(with: user)
        let realm = try! Realm()
        let identity = (user.identity)!

        let person = Person() = identity
        try! realm.write {
          realm.add(person, update: true)

        self?.performSegue(withIdentifier: "showProfile", sender: self)

The Person successfully created on the cloud server.

In the next viewcontroller i like to fetch the object based on the id:

let realm = try! Realm()
guard let uuid = SyncUser.current?.identity! else { return }
let person = realm.objects(Person.self).filter("id = %@", uuid).first

try! realm.write {
  person?.name = "test"

The person is always nil I also tried the query the object with the primary key, but with no success.

The Person class looks like:

class Person : Object {

    @objc dynamic var id = ""
    @objc dynamic var created: Date = Date()
    @objc dynamic var name = ""
    @objc dynamic var email = ""
    @objc dynamic var avatar : Data?

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

It seems like the Person object is not added to realm, or added to another realm file. But it is visible on the Realm Cloud Browser. Anybody a idea whats wrong?


@rouuuge Are you opening a synced Realm? I don’t see a SyncConfiguration - take a look here for an example:


yes I think so:

public func setDefaultRealmConfiguration(with user: SyncUser) {
        var config = user.configuration(realmURL: Constants.defaultRealmURL)
        config.objectTypes = [Team.self, Person.self]
        Realm.Configuration.defaultConfiguration = config


I made a much simpler example app: I just have one Viewcontroller with following code:

class ViewController: UIViewController {

    override func viewDidLoad() {
        let creds = SyncCredentials.usernamePassword(username: "admin", password: "test")
        SyncUser.logIn(with: creds, server: Constants.AUTH_URL, onCompletion: { (user, err) in
            if let user = user {
                var config = user.configuration(realmURL: Constants.REALM_URL)
                config.objectTypes = [Person.self]
                Realm.asyncOpen(configuration: config, callback: { (realm, error) in
                    guard let realm = realm else {return}
                    let objects = realm.objects(Person.self)
                    print(objects) // always empty why???
                    try! realm.write {
                        let p = Person(id: "test")
                    print(objects) // one object

as with my other problem the person is successfully added to the cloud. but when I restart the app the objects are empty on the first query. Maybe I miss understanding something with the synched realms?


Try this and see if it makes any difference. The Realm documentation in outdated and incorrect in some cases.

let results = realm.objects(Person.self)
let subscription = results.subscribe()


Try it without this line:

config.objectTypes = [Person.self]


thank you!

results.subscribe resolve my problem!