Deleting Database with large number of attachments

53 views
Skip to first unread message

Scott Ahten

unread,
Apr 4, 2014, 2:26:39 PM4/4/14
to mobile-c...@googlegroups.com
I'm working a database that has roughly 2,500 small attachments. When I call [CBLDatabase deleteDatabase:] the app's UI hangs for several seconds.

When I enabled "-LogCBLDatabase YES" on launch, the hang appears to occur when calling [[NSFileManager defaultManager] removeItemAtPath: path error: &error] for the attachments directory. 

Is there a safe way to push this work off to a background thread? 

Jens Alfke

unread,
Apr 7, 2014, 12:02:59 PM4/7/14
to mobile-c...@googlegroups.com

On Apr 4, 2014, at 11:26 AM, Scott Ahten <lightand...@gmail.com> wrote:

Is there a safe way to push this work off to a background thread? 

Yes, CBLManager has a -backgroundTellDatabaseNamed:to: method that will run a block on the background thread that’s used for replication. The gotcha with that method is that, since the background thread has its own objects, you must use the CBLDatabase instance that’s passed into the block instead of the one you use on the main thread.

—Jens

Scott Ahten

unread,
Apr 7, 2014, 5:13:08 PM4/7/14
to mobile-c...@googlegroups.com
Thanks Jens. 

I was just about to post this as a possible solution. 

To ensure the database is not accessed while the background thread is working, I call stop on both pull and push replications beforehand and set the main thread's reference to the database to nil immediately after. 

Jens Alfke

unread,
Apr 7, 2014, 5:49:24 PM4/7/14
to mobile-c...@googlegroups.com

On Apr 7, 2014, at 2:13 PM, Scott Ahten <lightand...@gmail.com> wrote:

To ensure the database is not accessed while the background thread is working, I call stop on both pull and push replications beforehand and set the main thread's reference to the database to nil immediately after. 

That’s not necessary. It’s OK for multiple threads to access the same database file — SQLite takes care of that — as long as each thread uses its own set of objects to do it.

What you’ll find is that the replicator will stop during the compaction, because they’re on the same thread, and that database access from other threads may block for a while depending on how long the compact process’s transactions take.

—Jens
Reply all
Reply to author
Forward
0 new messages