

userDomainMask, appropriateFor: nil, create: true) So, pulling that together with the above points, you get something like: let folder = try! fault The compiler will make sure that you properly return when an error condition occurs. I would generally advise using guard in situations where you want to exit upon failure.

Without that, it will be exceedingly difficult to debug problems. If a SQL statement fails for any reason, if you are going to print an error, you really should include the sqlite3_errmsg. But if you move the defer to the right place, that observation is moot. You want to move it out of there, anyway, because defer will run at the end of the current scope (at the end of your if statement in your current implementation), not at the end of the function. So I would move the defer out of that if statement. Whether or not an error occurs when it is opened, resources associated with the database connection handle should be released by passing it to sqlite3_close() when it is no longer required. If opening a database failed for any reason, you still need to call sqlite3_close. documents folder, because that is reserved for user-facing files.Īlso, we would also generally use use the create option of true. applicationSupportDirectory, which is not deleted when storage runs low and is backed up when you back up your device. Are you sure you want to delete your app database in this scenario? Usually we would use. cachesDirectory is deleted when your device runs low in storage.
