Multiple Realm Collections in kotlin

I’m creating an Ecommerce App, and i want it’s db to be collections of separate articles ,

class Articles : RealmObject { // Main Class
    @PrimaryKey
    var _id: ObjectId = ObjectId.invoke()
    var title: String = ""
    var price: Int = 0
}

I want my db to be collections (Files) of : Clothes , Parfums , Accessories… Each collection has the same fields as Articles above

I tried creating Multiple Dublicated Classes : class Clothes , class Parfums …etc But that doesn’t seems practicle

Collections are not files - they are groups of related objects. That being said, technically they could be separate (Realm) files but that’s probably not what you’re after, or needed.

One option to consider simply adding a “type” property to your Article class - noting that “Articles” is plural and Realm objects are singular objects so better to name them accordingly.

class Article: RealmObject {
    @PrimaryKey
    var _id: ObjectId = ObjectId.invoke()
    var title: String = ""
    var price: Int = 0
    var type: String = "" //the object type; Clothing, Parfume, Accessory etc
}

That allows you to look at all of the Article Objects as a group, or by filtering on the type property, view and work with them as a sub group (collection)

Realm objects can also be subclassed if the subclasses need different functionality than the parent class. For example a Parfume object would not need a clothing type (pant, shirt etc) so here would be a Clothing Type subclass that has all of the attributes and properties of the parent class but also specific properties that only apply to Clothing

class ClothingClass: Article {
  var clothing_type: String //pant, shirt etc
}

Keeping in mind that none of this requires separate “files”

Thanks for ur reply , but in my case I strictly need a separate realm files for a unique fetching Articles algorithms , Is there a way ?! Using enum classes or something …

Sure! Realm easily supports multiple local Realms - keeping in mind that relationships and queries cannot be cross-realm; they are limited to a single Realm file. However, if this is going to be a sync’d Realm, that changes the answer a bit, and since the thread is marked Android and ecommerce, is it correct this will be sync’d?

Before providing any further advise or discussion, it would be important to include details about your use case because as mentioned, separating Realm into discreet files has a number of downsides and may not provide any advantages.

Can you provide details about this statement? What kind of algorithm would require a unique Realm file?

I decided to adopt Mongodb approach and reconstruct my db , thank u so much Jayson for u help ^^ , hope u all the best