Is it safe to delete sqlite's WAL file?

前端 未结 6 896
小蘑菇
小蘑菇 2020-12-08 08:16

I have a strange problem with Core Data in an iOS app where sometimes the WAL file becomes huge (~1GB). It appears there are other people with the problem (e.g. Cor

6条回答
  •  猫巷女王i
    2020-12-08 08:22

    You should never delete the sqlite WAL file, it contains transactions that haven't been written to the actual sqlite file yet. Instead force the database to checkpoint, and then clean up the WAL file for you.

    In CoreData the best way to do this is to open the database with the DELETE journal mode pragma. This will checkpoint and then delete the WAL file for you.

    NSDictionary *options = @{ NSSQLitePragmasOption: @{ @"journal_mode": @"DELETE"}};
    [psc addPersistentStoreWithType:NSSQLiteStoreType
                      configuration:nil
                                URL:_storeURL
                            options:options
                              error:&localError];
    

    For sanity sake you should ensure you only have one connection to the persistent store when you do this, i.e. only one persistent store instance in a single persistent store coordinator.

    FWIW in your particular case you may wish to use TRUNCATE or OFF for your initial database import, and switch to WAL for updates.

    http://www.sqlite.org/pragma.html#pragma_journal_mode

提交回复
热议问题