CBLModel isNew

35 views
Skip to first unread message

Martin Palatnik

unread,
Sep 30, 2014, 11:25:12 AM9/30/14
to mobile-c...@googlegroups.com
Hi, 

I'm trying to check is a CBLModel is new (never being saved). However isNew is always false. 
When I checked the code it makes sense because I'm passing to it a non null document. However that document is new as it was just created. Is this a bug or am I using the API in the wrong way? I just want to check if a model (or it's underliying doc) has never being saved. 

Below the CBLModel code doing that:

- (instancetype) initWithDocument: (CBLDocument*)document
{
    self = [super init];
    if (self) {
        if (document) {
            LogTo(CBLModel, @"%@ initWithDocument: %@ @%p", self.class, document, document);
            self.document = document;
            [self didLoadFromDocument];
        } else {
            _isNew = true;
            LogTo(CBLModel, @"%@ init", self);
        }
    }
    return self;
}


Thanks,
Martin

Jens Alfke

unread,
Sep 30, 2014, 1:42:52 PM9/30/14
to mobile-c...@googlegroups.com

On Sep 30, 2014, at 8:25 AM, Martin Palatnik <mar...@gmail.com> wrote:

I'm trying to check is a CBLModel is new (never being saved). However isNew is always false. 
When I checked the code it makes sense because I'm passing to it a non null document. However that document is new as it was just created.

.isNew will return YES if the model was created by -initWithNewDocumentInDatabase:, but not if it was created with +modelForDocument:, even if the document has no revisions. That's arguably a bug.

A reliable test that will do what you want is
model.document.currentRevision != nil

—Jens

gwis...@kpd-i.com

unread,
Dec 5, 2015, 2:26:34 PM12/5/15
to Couchbase Mobile
On Tuesday, September 30, 2014 at 1:42:52 PM UTC-4, Jens Alfke wrote:
.isNew will return YES if the model was created by -initWithNewDocumentInDatabase:, but not if it was created with +modelForDocument:, even if the document has no revisions. That's arguably a bug.

Incidentally, I'm also seeing .isNew returning false when the model was just created with initWithNewDocumentinDatabase, which surprises me. The currentRevision is nil, though, so I can go that route too.
Reply all
Reply to author
Forward
0 new messages