fuzzygoat
fuzzygoat

Reputation: 26223

Does Core Data database close after app exit?

I have been looking at a section of code in my current application that checks on application startup if the Core Data database is already open. The database is only accessed by the single app (and a single user) so it can't have been opened from another source. One thing I don't know for sure is if the application exits unexpectedly does the database close when this happens?

I guess I am asking that in a single user system can there be a situation where the user starts the application and the database is already open? In testing I have never seen the "its already open" code called, so I am just curious if I need it at all?

Upvotes: 2

Views: 1058

Answers (1)

Olaf
Olaf

Reputation: 3042

On iOS (as a single-user-app-is-active-at-a-time-OS) your app is the one that will "open" and "close" the database.

If the app closed unexpectedly all changes that your app did not at persist that moment will be lost. The app will not open the DB unless you tell it to (i.e by setting up the NSPersistantStorageController and a Context).

The "is already open" code will come in handy when the DB opening can be triggered at different stages from within the app. So if there is only one place where you open it you won't need the code. It wont hurt, though.

The thing that often catches me out is when I use something like the sqlite database browser or Liya to monitor the DB during debugging and the browser is blocking updates to the store. But that is during debugging, not during normal usage.

Upvotes: 1

Related Questions