Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS CoreData - are there any disadvantages to enabling sqlite WAL / Write-Ahead Logging

At the WWDC 2013 session '207: What's New in Core Data', they mention that you can enable SQLite WAL by passing an options dictionary when adding a persistent store:

@{ NSSQLitePragmasOption: @"journal_mode = WAL" }

(which is available on iOS4+ and will be the default for future iOS versions).

I'm wondering whether this would generally be a good thing to enable in my app for earlier iOS versions too.

I've consulted the SQLite page about write ahead logging and the disadvantages they mention, most of them seem not to apply to iOS apart from:

  • WAL might be very slightly slower (perhaps 1% or 2% slower) than the traditional rollback-journal approach in applications that do mostly reads and seldom write.

pretty much all the advantages do sound like they'll probably be a benefit on iOS:

  • WAL is significantly faster in most scenarios.
  • WAL provides more concurrency as readers do not block writers and a writer does not block readers. Reading and writing can proceed concurrently.
  • Disk I/O operations tends to be more sequential using WAL.
  • WAL uses many fewer fsync() operations and is thus less vulnerable to problems on systems where the fsync() system call is broken.

I'm asuming (perhaps subject to doing some checks on my app to make sure it doesn't slow things down) that this would be a good thing to enable, but is there any downside I should watch for or any known issues?

like image 246
JosephH Avatar asked Jul 05 '13 10:07

JosephH


People also ask

What is SQLite write-ahead log?

To make SQLite even faster you can enable a feature called Write-Ahead Logging (aka WAL). Normally when you update your SQLite Database within a transaction, the original data is copied to a separate rollback file. The new data is written directly to the DB file. This results in two disk writes for every DB change.

What is WAL mode in SQLite?

The persistence of WAL mode means that applications can be converted to using SQLite in WAL mode without making any changes to the application itself. One has merely to run "PRAGMA journal_mode=WAL;" on the database file(s) using the command-line shell or other utility, then restart the application.

What is WAL format?

The write-ahead log or "wal" file is a roll-forward journal that records transactions that have been committed but not yet applied to the main database. Details on the format of the wal file are describe in the WAL format subsection of the main file format document.

Is Coredata a database?

Core Data is not a database. Core Data is a framework for managing an object graph. An object graph is nothing more than a collection of interconnected objects. The framework excels at managing complex object graphs.


1 Answers

http://pablin.org/2013/05/24/problems-with-core-data-migration-manager-and-journal-mode-wal/ suggests that their could be issues with migrations, in particular:

When you use a Migration Manager, Core Data will create a new database for you, and start copying the entities one by one from the old DB to the new one.

As we are using journal_mode = WAL, there’s an additional file besides DB.sqlite called DB.sqlite-wal.

From what I can tell, the problem seems to be that Core Data creates a temporary DB, inserts everything there, and when it renames it to the original name, the -wal file is kept as a leftover from the old version. The problem is that you end up with an inconsistent DB.

(also mentioned on https://github.com/magicalpanda/MagicalRecord/issues/490 - which suggests that if you use magical record then it is already defaulting to WAL )

like image 151
JosephH Avatar answered Sep 19 '22 11:09

JosephH