smat88dd
smat88dd

Reputation: 2388

NSPersistentContainer doesnt create database file

I'am creating a NSPersistentContainer to save some data, but even though calling save() on the context doesnt create any database file.

The code I'm using for creating the NSPersistentContainer is:

let container = NSPersistentContainer(name: "Model")

let description = NSPersistentStoreDescription()
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true

container.persistentStoreDescriptions = [description]

container.loadPersistentStores { ... }

Upvotes: 0

Views: 302

Answers (1)

smat88dd
smat88dd

Reputation: 2388

I was expecting that setting the auto migration options via NSPersistentStoreDescription wouldnt be a problem and that the Container would use the default store file path; but it wasnt.

Turns out there is no fallback to that default path, so one would have to provide your own path, which fixes the problem:

let container = NSPersistentContainer(name: "Model")

let dbURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last!.appendingPathComponent("DB.sql")
let description = NSPersistentStoreDescription(url: dbURL)
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true

container.persistentStoreDescriptions = [description]

container.loadPersistentStores { ... }

Upvotes: 2

Related Questions