Kádi
Kádi

Reputation: 2916

Do not map Realm object into Realm table

Basically my problem is that I want to define a realm object (RealmSwift.Object subclass) and I do not want it to appear in the Realm database, instruct Realm to skip this one.

For example:

I want to implement a synchronisation logic in a private pod with dynamic list of objects that are all subclassed from an object, called SyncableObject.

I have two classes that I register into my sync module (ConcreteClassA and ConcreteClassB) like SyncModule.register([ConcreteClassA.self, ConcreteClassB.self]).

Now Realm detects that I have 3 RealmSwift.Object subclasses and creates 3 different tables.

I want a solution to instruct Realm, not to create the table for the SyncableObject (intermediate object) class, like overriding a class function or something like that.

Upvotes: 1

Views: 603

Answers (3)

Kádi
Kádi

Reputation: 2916

By upgrading the previous solutions, I figured that this will do the trick to not include the base object and do include the subclasses:

/**
 Do not include SyncableObject into schema, only the subclasses
 */
public override class func shouldIncludeInDefaultSchema() -> Bool {
    return SyncableObject.className() != self.className()
}

Upvotes: 1

Gerard
Gerard

Reputation: 4848

Here's the answer in Swift:

override class func shouldIncludeInDefaultSchema() -> Bool {
   return false
}

Upvotes: 0

jpsim
jpsim

Reputation: 14409

You'll want to override +[RLMObject shouldIncludeInDefaultSchema]. From Realm's source:

// Returns whether the class is included in the default set of classes persisted in a Realm.
+ (BOOL)shouldIncludeInDefaultSchema;

Upvotes: 2

Related Questions