Core Data Appears to Lose Data After Xcode Upgrade - swift

For the second time, my core data app has "lost" (explanation below) the data. I'm not positive this has to do with the update of Xcode, but I did update to 9.4 today (I don't remember whether there was an update involved the last time this happened). I entered data yesterday, then closed the app, re-opened the app, and everything was fine; all the data was there. Today when I opened the app, there was no data. My app prints out the location of the database (print(container.persistentStoreDescriptions)), and when I look at the sqlite file there it has 25 kB.
Searching through the ridiculously deep folder path, I found a file that said it was modified yesterday, and when I look at the sqlite file there (with DB Browser for SQLite), the data is all there (also 25 kB). It seems as if Xcode has forgotten where it put the data. Has anyone else experienced this?
How would I go about fixing this? I guess I could copy the SQLite database from where it actually is to the place Xcode thinks it is (the other files there also, .sqlite-shm and .sqlite-wal?).

after the update of Xcode, the device that initially ran was iPhone 8, whereas, I had been running it on an iPad simulator before the update
Each simulator gets its own folder with its own sqlite files. So if you want to see the data that was in the iPad simulator before, you need to run the same iPad simulator from the new Xcode. If you copy sqlite files from one simulator folder to another, be sure to copy the -shm and -wal files too.

Related

iPhone SQLite Database not updating

I just updated my sqlite database (added some new records) - saved it, reimported it into my Xcode project, and re-ran my app - all the new data showed up perfectly well on the iPhone Simulator as expected.
However, after installing the app on my iPhone, I'm still getting the old data that was bundled with the previous version of the app.
I of course deleted the older version of the app from the iPhone first, assuming it would thereby destroy its "Documents" folder and the older db file that was in it - but for some reason its still reading (or displaying) the old data - which doesn't make sense. Any ideas?
Try rebooting your device after you make database changes, and then seeing what sort of results you get.

sqlite3 fails to read database for me, but not for same commit on another machine

BACKGROUND
We have an iPhone app that uses sqlite for a large database. We use FTS3, full text search, so we have to "roll our own" sqlite instead of using the library included in the iOS SDK. We use the amalgamated sqlite.c file, and we use FMDB as an Obj-C wrapper.
THE PROBLEM
My project compiles, but it crashes on an exception that I've installed to detect if the database is malfunctioning (more on that in a bit). My partner, however, is merrily coding away, with no problems - on the EXACT SAME commit revision as me. That's what's weird.
Specifically, the app does the following on first load:
Copy the database into the user's document directory so that it is writeable. (working OK)
Open the database using FMDB's databaseWithPath: followed by open.
Calling goodConnection on FMDB at this point returns YES - the database is indeed open and working.
Calling SELECT * FROM sqlite_master WHERE type = 'table' returns no records. (there should be many tables)
WHAT I'VE TRIED
Now, if you ask sqlite to "open" a database that doesn't exist -- it'll just create one. That's what this behavior looks like. So, I opened up a Terminal window, navigated to my app's Documents directory on the simulator, and typed:
Makbook:Documents makdad$ sqlite3 myDB.db
Which runs sqlite3, and, of course, everything looks fine. All my data is there.
I stepped through line-by-line of FMDB's open method to make sure I was opening the right database. It looks like I certainly am.
First thing I tried was downgrading to Xcode 3.2.6. Same problem, so I think I've ruled out an Xcode 3/4 difference in compiling (although I suspected that at first).
Does anyone have any debugging-fu to try, or any knowledge as to what my problem could be?
Had the same issue with our custom compiled SQLite3. I found a solution to this problem on this blog http://regularrateandrhythm.com/regular-rate-rhythm-blog/sqlite3-fts-in-IOS4.html
The problem is with the iOS 4.3 SDK Simulator. I had neglected to test the iOS device itself, and it works.
To support older iOS versions, we use the -weak_library /usr/lib/libSystem.B.dylib linker flag. Removing that flag on iOS Simulator 4.3 stops ALL the weird behavior relating to sqlite3.
The reason my partners weren't having the problem is that they are running Xcode 3.2.5, which is 4.2. Even when I downgraded to Xcode 3, I still had 4 installed - so 3 was using the 4.3 SDK as well.
I am going to open a new question about the new issue, as this question is so far off the actual issue :)

Contents of SQLite dbase on iPhone are deleted after crash

I have this weird problem where I lose my data after a crash.
What happens is the following:
I insert a product in the app -> product is saved to the sqlite database.
The app crashes. At this moment the product is still in the database (I double checked this on simulator and iphone device with ftp to get my sqlite database off my phone.)
At the moment of reinitiating my app, the contents of the sqlite are removed. And an empty sqlite with the same tables etc is replaced.
I wonder why and how to fix this problem.
Thanks in advance,
Lewion.
I presume on start-up you're checking for the presence of the database in your document directory and copying a "clean" copy from your bundle if none is present.
Are you sure the problem doesn't lie in the logic you're using there?

Connecting CoreData to my App on an iPhone Device

I apologize ahead of time for what I'm sure is a complete newbie lapse. Running my iPhone app on iPhone simulator - no problem. But I loaded the app on an iPhone device for the first time and it appears as if the SQLite database I'm using (NSManagedObjectContext) isn't connected or didn't upload. The app installs but with no data. How do I get it all to upload and work on the device?
I appreciate any help.
lq
The first rule with Core Data is that you should never ever touch the store directly. It should be considered a black box and only accessed via the the Core Data APIs. Accessing the database directly is unsupported and can lead to data corruption.
What you should do instead is create a trivial desktop application (or command line is you are just importing from another source) and enter the data there. Once the data is entered then copy the sqlite file to your mobile application and include it in the bundle. That way you are accessing the file from Core Data. Since Apple can and does change the structure of that file without notice your application will be future proof.
update
If you have created a database using Core Data you can then take that sqlite file and add it to your Xcode project for your iPhone application. Xcode will then copy it into the app bundle when it compiles the project.
Looking a little deeper I found a link that answers my own question. So anyone with the same question, this solved the issue for me:
http://ablogontech.wordpress.com/2009/07/13/using-a-pre-populated-sqlite-database-with-core-data-on-iphone-os-3-0/

SQLITE no such table: error IPhone

Im have been using sqlite as a store in my app and everything was going fine problem is i changed my database schema deleted the old one and then inserted the new one in and now the application no longer loads any data despite all of the sql statements working fine whenever i run them in the firefox plugin. I have done a build clean and reset the simulator as well as delete the app in the simulator does anyone have any ideas whats going on?
-Have you copied the new database to the documents directory in the appdelegate?
-After deleting the app from the simulator, and do a clean all, it should copy the database to the app's documents directory. Or if you won't change the database, you can delete the old and copy the new one every start.