Setting the type property automatically when instantiating a new CBLModel

78 views
Skip to first unread message

Sebastien ARBOGAST

unread,
Jan 6, 2015, 10:25:44 AM1/6/15
to mobile-c...@googlegroups.com
I have device a model class that extends CBLModel. I have also registered this class with database.modelFactory. I was hoping that thanks to that, the type property of my document would be set automatically when I create it using let book = Book(newDocumentInDatabase:database), but it is not. And I tried overriding init like specified in the doc:

init!(document: CBLDocument!) {

        super.init(document: document)

        self.type = "book"

    }

But the Swift compiler complains that it cannot find the CBLDocument class. Did I miss something?

---
Sébastien Arbogast
http://sebastien-arbogast.com

Mark

unread,
Jan 6, 2015, 10:39:37 AM1/6/15
to mobile-c...@googlegroups.com
You'll either have to do it in -initWithDocument, -didLoadFromDocument or, as of 1.1, -awakeFromInitializer (see FYI: CBLModel API change [iOS]).

Depending on how you create your model, you can use the "isNew" flag to see if the model's underlying document is new and set type. Note that if you create your document yourself and then create a model (say if you need your own docID) you'll have to either check if type is already set and then set it.

I haven't tried this in swift yet, but it works in Obj-C.
> --
> You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/CAG1DMa_CnZJ4daE_KMTwVbm-th_v9DQMCUEeEPmZ2ya8Wvd1QA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Sebastien ARBOGAST

unread,
Jan 6, 2015, 10:43:43 AM1/6/15
to mobile-c...@googlegroups.com
All this seems overly complicated. Why doesn't +modelForDocument do it automatically? It has the information from the modelFactory. It uses it to load a model of the right type from the document, it would make sense that it saves a document with the right type from a model, don't you think?

---
Sébastien Arbogast
http://sebastien-arbogast.com

Jens Alfke

unread,
Jan 6, 2015, 11:21:54 AM1/6/15
to mobile-c...@googlegroups.com
On Jan 6, 2015, at 7:43 AM, Sebastien ARBOGAST <sebastien...@gmail.com> wrote:

All this seems overly complicated. Why doesn't +modelForDocument do it automatically?

Because no one's thought of implementing it before, and because we're a small team and there's so much stuff to do. :/
Please file an issue requesting this functionality. (And if you're interested in implementing it, a pull request would be welcome.)

It has the information from the modelFactory. It uses it to load a model of the right type from the document, it would make sense that it saves a document with the right type from a model, don't you think?

Well, the "type" property is generally just a convention, not anything built-in. But you're correct that the ModelFactory uses this convention, so it would make sense for Model to use that too.

—Jens

Sebastien ARBOGAST

unread,
Jan 6, 2015, 11:25:31 AM1/6/15
to mobile-c...@googlegroups.com
Thanks a lot Jens. No worries: I know you have a lot on your plate and what you are building is simply awesome. I just wanted to make sure I hadn't missed something obvious before diving into a pull request. But I'll definitely do that. By the way, about the small team problem, you should have a look at your job application queue ;)

---
Sébastien Arbogast
http://sebastien-arbogast.com

--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.

Sebastien ARBOGAST

unread,
Jan 6, 2015, 1:58:14 PM1/6/15
to mobile-c...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages