Trying to save just before macOS app quits

7 views
Skip to first unread message

Brendan Duddridge

unread,
Oct 16, 2017, 3:33:19 AM10/16/17
to Couchbase Mobile
Hi,

So I've got code to trap when a window closes and when my application quits.

I'm observing the NSApplicationWillTerminateNotification notification and saving to the database before the application quits.

Unfortunately I'm finding that the CBLManager is being closed before I get a chance to save to the database.

I looked in the CBL source and the only place I found NSApplicationWillTerminateNotification was in a couple of places that didn't seem like it could cause the database to be closed. One was in CBLRemoteLogging and the other was in DDLog.

Here's what I see right before my termination handler executes:

01:17:11.221| Database: CLOSING CBLManager[0x60c00007f780 /Users/brendan/Library/Containers/com.tapzapp.tapforms-mac/Data/Documents/Contacts Import.tapforms] ...

01:17:11.221| Database: Closing <0x60f000113fe0> /Users/brendan/Library/Containers/com.tapzapp.tapforms-mac/Data/Documents/Contacts Import.tapforms/db-5764caa159814fc7aec294c25c0ec60d.cblite2

01:17:11.224| Database: CLOSED CBLManager[0x60c00007f780 /Users/brendan/Library/Containers/com.tapzapp.tapforms-mac/Data/Documents/Contacts Import.tapforms]


So I'm wondering if there's some other action that's causing the database file to be closed prematurely?

I obviously do want it to close if I quit the app. I just want to save my data before that happens.

Thanks,

Brendan

Jens Alfke

unread,
Oct 16, 2017, 3:09:50 PM10/16/17
to mobile-c...@googlegroups.com
Try setting a breakpoint at “-[CBLDatabase close]” and look at the backtrace when it’s hit when you quit the app.

(I think that’s the correct selector. If it doesn’t get hit, try -close: or -_close)

—Jens

Brendan Duddridge

unread,
Oct 16, 2017, 5:38:32 PM10/16/17
to Couchbase Mobile
Hi Jens,

Thanks for your response.

I tried that.

Unfortunately for some reason I can't get CBL to stop on any breakpoints.

I'm building (command-b) from the CBL Mac scheme and then going to Products, show in Finder, then copying the CouchbaseLite.framework into my project.

Then I run my app, then quit my app. Xcode only stops at my own breakpoints. None from the Couchbase Lite project I have opened also.

I'm pretty sure this used to work a couple Xcodes ago. I'm using Xcode 9 now on High Sierra.

Thanks,

Brendan

Brendan Duddridge

unread,
Oct 16, 2017, 5:48:08 PM10/16/17
to Couchbase Mobile
Ok. never mind.

Putting a symbolic breakpoint at -[CBLManager close] dumped out the backtrace and I saw the mistake I made.

Turns out I was closing the database in applicationShouldTerminate which executes before applicationWillTerminate.

Sorry for the post. And thanks for your help.

Brendan
Reply all
Reply to author
Forward
0 new messages