didLoadFromDocument not called on subclassed CBLModel objects in Swift


I have an iOS Swift project where I use CBLModel objects, and I have problems receiving model updates on some objects. On objects that are direct subclasses of CBLModel the didLoadFromDocument method is called whenever the underlying document is changed, but this doesn’t happen when the object is a subclass of a subclass of CBLModel.

Here is some (hopefully) relevant sections of my code:

class EventModel: CBLModel {
    @NSManaged var title: String
    @NSManaged var date: Int64

    override func didLoadFromDocument() { 
        print("EventModel: didLoadFromDocument")  // <--  This gets called when the document is updated
class EventElementModel: CBLModel {

    @NSManaged var centerX: CGFloat
    @NSManaged var centerY: CGFloat
    @NSManaged var width: CGFloat
    @NSManaged var height: CGFloat
    @NSManaged var rotation: CGFloat
    override func didLoadFromDocument() {
        print("EventElementModel: didLoadFromDocument")  // <-- This never gets called
class EventTextElementModel: EventElementModel {
    @NSManaged var text: String
    @NSManaged var fontName: String
    @NSManaged var fontSize: CGFloat
    @NSManaged var fontColor: String
    override func didLoadFromDocument() {
        print("EventTextElementModel: didLoadFromDocument")  // <-- This never gets called

Database initialization

    database = try! manager.databaseNamed("db")

    let factory = database.modelFactory
    factory?.registerClass(EventElementModel.self, forDocumentType: "eventElementModel")
    factory?.registerClass(EventTextElementModel.self, forDocumentType: "eventTextElementModel")

    let url = URL(string: "http://xxxx.xxx:4984/db")!
    let push = database.createPushReplication(url)
    let pull = database.createPullReplication(url)

    pull.continuous = true
    push.continuous = true


I solved this when I enabled “Model” logging. The object was deleted right after it was created because I forgot to store a reference to it. Now to works like a charm :slight_smile:

By the way, the correct logging tag for CBLModel is “Model” and not “CBLModel” as it says in the documentation.