How to Turn Off Core Data Write-Ahead Logging in Swift Using Options Dictionary

How to turn off Core Data Write-Ahead logging in Swift using options dictionary?

Swift dictionaries are strongly typed by default, but you can define the types your dictionary should accept.

var options = Dictionary<NSObject, AnyObject>()
options[NSMigratePersistentStoresAutomaticallyOption] = true
options[NSInferMappingModelAutomaticallyOption] = true
options["journal_mode"] = "DELETE"

[storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:[self databaseURL]
options:opitons
error:&error]

If you weren't sure what type of dictionary the function was expecting, just take a look at the function declaration :

func addPersistentStoreWithType(_ storeType: String!,
configuration configuration: String!,
URL storeURL: NSURL!,
options options: [NSObject : AnyObject]!,
error error: AutoreleasingUnsafePointer<NSError?>) -> NSPersistentStore!

It describes exactly the type of dictionary it expects -- Dictionary<NSObject, AnyObject>.

In fact, any dictionary that is bridged from Objective-C will be typed this way. From the Apple Docs:

Swift also automatically bridges between the Dictionary type and the
NSDictionary class. When you bridge from an NSDictionary object to a
Swift dictionary, the resulting dictionary is of type [NSObject:
AnyObject].

You can bridge any NSDictionary object to a Swift
dictionary because all Objective-C objects are AnyObject compatible.
Recall that an object is AnyObject compatible if it is an instance of
an Objective-C or Swift class, or if it can be bridged to one. All
NSDictionary objects can be bridged to Swift dictionaries, so the
Swift compiler replaces the NSDictionary class with [NSObject:
AnyObject] when it imports Objective-C APIs.

Likewise, when you use a
Swift class or protocol in Objective-C code, the importer remaps
Objective-C compatible Swift dictionaries as NSDictionary objects.

How to disable WAL journal mode

To disable WAL mode, set the journal_mode to DELETE

NSMutableDictionary *pragmaOptions = [NSMutableDictionary dictionary];
[pragmaOptions setObject:@"DELETE" forKey:@"journal_mode"];

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, pragmaOptions, NSSQLitePragmasOption, nil];
[_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]

How to enable DELETE MODE in iOS coredata sqlite using swift

Not sure I fully understand your question, anyway have you tried turning off journaling?

options["journal_mode"] = "OFF"

Hope this helps

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

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 )

Implementing Core Data validation methods in Swift

The documentation on this is very sparse and there isn't much information about it around the web. Adding here as reference for others what I was able to find via some digging and trail and error.

This seems to be working:

// Validating the 'name' attribute of NSManagedObject's descendant
func validateName(name: AutoreleasingUnsafeMutablePointer<AnyObject?>, error: NSErrorPointer) -> Bool {

if let name = name.memory as? String {
// do validation here when name is not nil
} else {
// when name is nil
return false
}
}


Related Topics



Leave a reply



Submit