SQLite data lost when killing the app #385

Closed
opened 2025-12-29 15:30:40 +01:00 by adam · 12 comments
Owner

Originally created by @odilebellerose on GitHub (Mar 30, 2022).

Hi,
when i create some objects by using

dataStack.perform( asynchronous: { (transaction) -> StoredDocument? in) })

, everything is working well i can see datas in my sqlite database. However, when i kill my app, the database is empty.

I've tried to save context on applicationWillTerminate but it doesn't change anything,

do {
	try dataStack.unsafeContext().save()
} catch {
			
}

Any idea of what's happens ?

Thanks you in advance for your answer.

Originally created by @odilebellerose on GitHub (Mar 30, 2022). Hi, when i create some objects by using ``` dataStack.perform( asynchronous: { (transaction) -> StoredDocument? in) }) ``` , everything is working well i can see datas in my sqlite database. However, when i kill my app, the database is empty. I've tried to save context on applicationWillTerminate but it doesn't change anything, ``` do { try dataStack.unsafeContext().save() } catch { } ``` Any idea of what's happens ? Thanks you in advance for your answer.
adam closed this issue 2025-12-29 15:30:40 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Mar 30, 2022):

I'm not sure I understand what you mean. You are sure that the SQLite database is created and populated correctly, but the file disappears after you terminate the app?

If so then something in your code is deleting the database manually, as CoreStore doesn't have any mechanism that automatically deletes the store.

@JohnEstropia commented on GitHub (Mar 30, 2022): I'm not sure I understand what you mean. You are sure that the SQLite database is created and populated correctly, but the file disappears after you terminate the app? If so then something in your code is deleting the database manually, as CoreStore doesn't have any mechanism that automatically deletes the store.
Author
Owner

@odilebellerose commented on GitHub (Mar 30, 2022):

I'm not sure I understand what you mean. You are sure that the SQLite database is created and populated correctly, but the file disappears after you terminate the app?

If so then something in your code is deleting the database manually, as CoreStore doesn't have any mechanism that automatically deletes the store.

The database still exists but records are deleted. Do you know if there is a way that CoreStore re-create sqlite database or erase the content ?

@odilebellerose commented on GitHub (Mar 30, 2022): > I'm not sure I understand what you mean. You are sure that the SQLite database is created and populated correctly, but the file disappears after you terminate the app? > > If so then something in your code is deleting the database manually, as CoreStore doesn't have any mechanism that automatically deletes the store. The database still exists but records are deleted. Do you know if there is a way that CoreStore re-create sqlite database or erase the content ?
Author
Owner

@JohnEstropia commented on GitHub (Mar 30, 2022):

Is the SQLite file URL constant or is it generated and somehow changes every launch?

The only reason CoreStore would reset a database is if the schema was updated AND the .recreateStoreOnModelMismatch option is set on the SQLiteStore

@JohnEstropia commented on GitHub (Mar 30, 2022): Is the SQLite file URL constant or is it generated and somehow changes every launch? The only reason CoreStore would reset a database is if the schema was updated AND the `.recreateStoreOnModelMismatch` option is set on the `SQLiteStore`
Author
Owner

@odilebellerose commented on GitHub (Mar 30, 2022):

I have a Singleton, with a start() func :

open func start() {
	
		let store = SQLiteStore(fileName: "documents_storage.sqlite")
		
		do {
			try self.dataStack.addStorageAndWait(store)
		} catch {
			
		}
	}

and i'm calling this function in the AppDelegate :

open func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  DocumentStorageManager.shared.start()

return true
}
@odilebellerose commented on GitHub (Mar 30, 2022): I have a Singleton, with a start() func : ``` open func start() { let store = SQLiteStore(fileName: "documents_storage.sqlite") do { try self.dataStack.addStorageAndWait(store) } catch { } } ``` and i'm calling this function in the AppDelegate : ``` open func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { DocumentStorageManager.shared.start() return true } ```
Author
Owner

@JohnEstropia commented on GitHub (Mar 30, 2022):

Are you using storyboards? If so you are likely accessing the DataStack before addStorageAndWait() returns. Storyboard initial ViewControllers get initialized before the UIAppDelegate's didFinishLaunching is called. At least if I recall correctly.

Put a breakpoint on the dataStack getter and the addStorageAndWait() call and see if it's getting accessed somewhere else before the store is added.

@JohnEstropia commented on GitHub (Mar 30, 2022): Are you using storyboards? If so you are likely accessing the `DataStack` before `addStorageAndWait()` returns. Storyboard initial ViewControllers get initialized before the `UIAppDelegate`'s `didFinishLaunching` is called. At least if I recall correctly. Put a breakpoint on the `dataStack` getter and the `addStorageAndWait()` call and see if it's getting accessed somewhere else before the store is added.
Author
Owner

@odilebellerose commented on GitHub (Mar 30, 2022):

Are you using storyboards? If so you are likely accessing the DataStack before addStorageAndWait() returns. Storyboard initial ViewControllers get initialized before the UIAppDelegate's didFinishLaunching is called. At least if I recall correctly.

Put a breakpoint on the dataStack getter and the addStorageAndWait() call and see if it's getting accessed somewhere else before the store is added.

I'm not using Storyboards and i tried to put breakpoints on functions using the dataStack it's seems ok. The addStorageAndWait() func seems to be called first.

@odilebellerose commented on GitHub (Mar 30, 2022): > Are you using storyboards? If so you are likely accessing the `DataStack` before `addStorageAndWait()` returns. Storyboard initial ViewControllers get initialized before the `UIAppDelegate`'s `didFinishLaunching` is called. At least if I recall correctly. > > Put a breakpoint on the `dataStack` getter and the `addStorageAndWait()` call and see if it's getting accessed somewhere else before the store is added. I'm not using Storyboards and i tried to put breakpoints on functions using the dataStack it's seems ok. The `addStorageAndWait() `func seems to be called first.
Author
Owner

@JohnEstropia commented on GitHub (Mar 30, 2022):

How are you confirming the SQLite file contents? Are you sure the perform() methods are completing successfully?

@JohnEstropia commented on GitHub (Mar 30, 2022): How are you confirming the SQLite file contents? Are you sure the `perform()` methods are completing successfully?
Author
Owner

@odilebellerose commented on GitHub (Mar 30, 2022):

If i open the SQLite file in a browser i can see my records. In fact, i'm using CoreStore to store documents that i'm going to upload by using operation queue and background task. If i keep the app open or put it in the background, everything is working well. That's why i can confirm SQLite file contents. If i kill the app and re-open it, my app should fetch documents in database and try to re-upload documents but they have been deleted.

@odilebellerose commented on GitHub (Mar 30, 2022): If i open the SQLite file in a browser i can see my records. In fact, i'm using CoreStore to store documents that i'm going to upload by using operation queue and background task. If i keep the app open or put it in the background, everything is working well. That's why i can confirm SQLite file contents. If i kill the app and re-open it, my app should fetch documents in database and try to re-upload documents but they have been deleted.
Author
Owner

@JohnEstropia commented on GitHub (Mar 30, 2022):

After restarting the app, do you still see the records in the SQLite browser?

@JohnEstropia commented on GitHub (Mar 30, 2022): After restarting the app, do you still see the records in the SQLite browser?
Author
Owner

@odilebellerose commented on GitHub (Mar 30, 2022):

No, i can't.

@odilebellerose commented on GitHub (Mar 30, 2022): No, i can't.
Author
Owner

@JohnEstropia commented on GitHub (Mar 30, 2022):

There's definitely something deleting your file (or a parent folder) somewhere. If you can isolate the issue in a shareable project I'll try to take a look.

@JohnEstropia commented on GitHub (Mar 30, 2022): There's definitely something deleting your file (or a parent folder) somewhere. If you can isolate the issue in a shareable project I'll try to take a look.
Author
Owner

@odilebellerose commented on GitHub (Mar 30, 2022):

Unfortunately i can't :/
Thanks you for your help, i'm going to try to figure it out.

@odilebellerose commented on GitHub (Mar 30, 2022): Unfortunately i can't :/ Thanks you for your help, i'm going to try to figure it out.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#385