Core Data and iOS 7: Different behavior of persistent store
Asked Answered
S

1

67

I'm preparing an update for a Core Data based app for fixes with iOS 7. I use Xcode 5 and iOS 7 SDK GM. However I realized a different behavior of the persistent store (which is a UIManagedDocument): Prior to iOS 7 builds there was only one file persistentStore in the documents folder (sometimes there was a second one persistentStore-journal).

In iOS 7 builds (clean installation) there are now three files for the persistent store:

  • persistentStore
  • persistentStore-wal and
  • persistentStore-shm

Did Apple change the journal mode by default to WAL now? I wonder if there is an impact on my app (think of users how update from the last version)? Would it be best to disable WAL - and if so, how can I do this with iOS 7/UIManagedDocument?

Spiraea answered 18/9, 2013 at 10:57 Comment(1)
Perhaps have a look at the "What’s New in Core Data and iCloud" session from WWDC 2013. You can download the PDF file from developer.apple.com/wwdc/videos. Apple changed the default journaling mode for the SQLite file from "rollback" to "write-ahead logging".Greatgrandaunt
F
98

Yes, Apple have changed the default journal mode to WAL for iOS7. You can specify the journal mode by adding the NSSQLitePragmasOption to the options when calling addPersistentStoreWithType:configuration:url:options:error. E.g. to set the previous default mode of DELETE:

NSDictionary *options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} };

In my experience WAL gives better performance, but also see this post:

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

Fattish answered 18/9, 2013 at 11:14 Comment(7)
Hey @Andy, there's slight spelling mistake in your code. It should read @"journal_mode".Hindman
@Hindman I took care of the typo in journal_mode check out sqlite.org/pragma.html for all options.Emboly
We have an app that users started to complain they lost their data when they updated to the latest version, which was compiled for iOS 7. Changing the journal mode back to DELETE fixed our problem.Bollworm
This is a horrible way to roll this feature out. Apple should have known better than to auto enable this feature. Now to create a backup of a database, users have to have both files.. I have had hundred of user loose data when they restored their ios devices from backups after a ios7 update only to find that the WAL file was missing so all the data since moving to ios 7 is gone... simply upgrading a device to ios7 changes the way all the app store their data. Who thought this was a good "default"Handbook
Add me to the list of burned devs who also didn't notice this issue in advance. The DELETE option also worked for me.Smoker
@Andy Etheridge i am already using like this, NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; Now if i replace this with NSDictionary *options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} }; If this works fine or any error will comes in the future?Soudan
Core Data on Mavericks will ignore the setting after a while, when it seems the Persistent Store reloads the store automatically.Quickstep

© 2022 - 2024 — McMap. All rights reserved.