Using Local NON sync Realm file

I’m integrating the MongoRealm into existing iOS App. In this App - I have a Local Writable Realm file with some objects that I don’t wish to participate in Realm Sync as this contains a lot of catalog data.

So I have a used a specific file name based Realm Configuration to initialise the Realm object.

var config = Realm.Configuration(schemaVersion: 83)
guard let fileUrl = config.fileURL else {
    throw AppError.invalidState("fileURL of realm config is nil")
}
config.fileURL = fileUrl.deletingLastPathComponent().appendingPathComponent("content.realm")
config.readOnly = false
let realm = try Realm(configuration: config)

With the above - I’m able to see the file created in the documents folder.
I’m also able to create a new object in that Realm using the following definition

class Grade: Object {
    @objc dynamic var id=""
    @objc dynamic var sno=0
    @objc dynamic var name=""
    @objc dynamic var version=0
    @objc dynamic var timestamp = Date()
    let linkedPrimarySkills = List<String>()
    override static func primaryKey() -> String? {
        return "id"
    }
    
    override static func indexedProperties() -> [String] {
        return ["sno", "name"]
    }
}

And when I query the above realm using the realm.objects(Grade.self) - It returns the object I wrote as expected.

But if I use the find by primary key method to pick a specific object -

let grade3 = realm.object(ofType: Grade.self, forPrimaryKey: "003")

it returns NIL.

Since the above class is NOT part of a Synced Realm - I don’t have a partition key. Also unlike the collections in Synced Realm - I’m using a simple primary key called id. All this code is from Classic Realm usage as a local data store.

So I’m puzzled how this basic functionality is broken ? Is it not possible to have a local (Non-sync) realm co-exist with a sync realm.

Thanks
Ram

While trying to further collect details on the above - I also did predicate queries

While let grade3 = realm.object(ofType: Grade.self, forPrimaryKey: "003") is returning NIL - I’m able to do use the filtered query let filtered = grades.filter(NSPredicate(format: "id == %@", "003")) to find the record .

So looks like a bug to me. It will be great if someone can help confirm?

Best regards
Ram

Have you committed the transaction before querying on the primary key? If I use createOrUpdateModified to add the object to Realm and then get all objects of that type before committing, I get one object. However, if I query for the object by its primary key, nil is returned.

Yes I’m not writing & immediately trying to read it. Write is committed. I’ve also closed the app & double checked by opening the Realm file in realm studio. Object is definitely there. But when I relaunch the app & query based on Primary key - its nil

@Ram_Sundaram This looks a correct to me, can you open an issue here with a repro case?

@Ian_Ward Yes I had done so yesterday even before I posted here.

Realm.object returns NIL object when queried by primary key in Realm 10.0 beta2 · Issue #6672 · realm/realm-swift · GitHub - sorry missed referring to that issue over here.

thanks
Ram

Just a quick follow up on this topic.

Like the OP, I also read this from the documentation

Realm Sync ignores any collections that lack a Realm Schema as well as any documents that lack a valid value for the partition key.

to mean that if the object does not have a partition key, it is ignored.

That is NOT what it means.

That statement is referring to documents on the server side only. e.g. If you add a realm object to your app, with the intention for example, of using it only locally, Realm will attempt to sync it anyway and when it doesn’t have a _partitionKey, the console will throw errors, your app will stop working and nothing will sync. You’ll then have to wipe the client to correct the issue.

Per support,

if you want to use Objects in your project that do not sync, you need to manually specify the objectTypes under Configuration

At this moment however, what that process is or how to make to make it work is a mystery. The documentation doesn’t specify that process or when to modify the objectTypes property - possibly before the first sync?

When I figure it out, I will post some sample code. Please feel free to contribute if you know.