Prevent saving of specific CBLModel object

20 views
Skip to first unread message

Brendan Duddridge

unread,
Aug 3, 2015, 4:30:25 PM8/3/15
to Couchbase Mobile
Hi,

In my app I create a couple of dummy CBLModel objects that I use to contain logic and references to other objects. I would like to prevent this object with a specific document ID from being saved to the database, even when using the saveAllModels method. Is that possible?

Thanks,

Brendan

Jens Alfke

unread,
Aug 3, 2015, 4:45:31 PM8/3/15
to mobile-c...@googlegroups.com

On Aug 3, 2015, at 1:30 PM, Brendan Duddridge <bren...@gmail.com> wrote:

In my app I create a couple of dummy CBLModel objects that I use to contain logic and references to other objects. I would like to prevent this object with a specific document ID from being saved to the database, even when using the saveAllModels method. Is that possible?

I don’t think there’s a way to do exactly that. The closest thing that comes to mind is to enable autosave for all your other models, then call -autosaveAllModels instead — that method only saves models with autosave enabled. (If you don’t want autosave to trigger on its own, you could set the interval to something ridiculously long.)

Do you want those objects to never be saved? (And if so, why are they CBLModels?)

—Jens

Jens Alfke

unread,
Aug 3, 2015, 5:05:07 PM8/3/15
to mobile-c...@googlegroups.com
Oh, actually it’s pretty easy to do what you want. The implementation of -[CBLDatabase saveAllModels:] is very simple and uses only public API:

- (BOOL) saveAllModels: (NSError**)outError {
    NSArray* unsaved = self.unsavedModels;
    if (unsaved.count == 0)
        return YES;
    return [CBLModel saveModels: unsaved error: outError];
}

So you can easily write your own method that does the same, except filtering the `unsaved` array to remove the model(s) you don’t want to save.

—Jens

Brendan Duddridge

unread,
Aug 3, 2015, 8:31:47 PM8/3/15
to Couchbase Mobile
Hi Jens,

Yes, I don't want the models to ever be saved.

I suppose I could implement them in a different way. They were just a place to group a couple of "forms" in my database which contain a list of favourite records and a list of records that have alerts set on them. I group them within a category called "Favourites & Alerts". Within that I have the Favourites form and the Alerts form. I'm using an NSOutlineView to display them in a tree format and it was nice to use the same entity types as I use for real user defined categories and forms. A Category is just a collection of Form objects (not to be confused with objective-c categories). So I didn't need them stored in the database or to be able to be edited by the user.

Brendan Duddridge

unread,
Aug 3, 2015, 8:32:25 PM8/3/15
to Couchbase Mobile
Hi Jens,

Ah ok, great. I'll give that a shot and see how it goes.

Thanks!

Brendan
Reply all
Reply to author
Forward
0 new messages