Core Data got empty results in WAL journal_mode after app launch in iOS7, data lost - ios

My app is using Core Data framework with storage type of sqlite . I received several feedback commenting that user data stored in Core Data is lost, it seems the sqlite file read fail. After i got the sqlite files from user, there are 3 files (.sqlite, .sqlite-wal, .sqlite-shm).
But only empty results can be queried in these sqlite files.
What I got from the users is they may power off their iPhone while the app is active.
I just noticed Core Data begin use WAL as the default journal_mode in ios7, while it's default as DELETE prior to ios7. I didn't set up the journal_mode manually in my code.
I'm not sure what is the root cause of this issue. But data lost is unacceptable to users anyway. Did anyone happen to meet this issue before?
Should I set up the journal_mode to DELETE manually for all iOS version?
Thank you!
Update:
User reported it works normally on previous day, but then everything is lost on next day.

Related

Copying Sqlite database file content in CoreData

I'm working for an iOS app that was earlier developed using phone gap. It is having a sqlite database for storing data. But now while developing the same app in native, I'm using core data for storing the data.
Now when the new native app replaces the old phone gap app on user's device, I want to copy data from already existing sqlite file into core data.
So when I run the application on device with phone gap build pre installed my app(native) replaces the old build as I'm using same bundle ID but I'm not able to find out the path to that sqlite file. Does sqlite file still exists in documents directory?
If you have an example old database file still installed on one of your devices you could try downloading the App off the device using the Device manager in Xcode.
Then you open up the bundle and search for your file. Then if you know its location / name you can simply access it, read it using some SQLite library and load the data into your CoreData backing store. Keep in mind you either want to mark it in NSUserDefaults or delete the old DB all together so your app does not keep on migrating ( And then maybe accidentally deleting new data from a user. ) I would choose for keeping the old one around for at least a version or 2 so you can verify that your migration works without bugs / deleting user data in the process.
Tip: It is probably a good idea depending on how big your database is to show the user some kind of progress or "migrating / optimizing db" while you're doing this. So you don't end up with the user adding more data to the database before you're done migrating.

MagicalRecord SQLite file empty

I am having problem reading a file. I'm using MagicalRecord as my CoreData wrapper. I successfully save or update object. With my NSLog I can see it, everything is fine and I can use it in my database. But every time I want to see SQLite file with my application (I'm using Datum LE), file is empty. I cannot access that file directly within my Library folder in my app. I copy that file to my desktop and it is empty. What am I doing wrong? So once more, everything inside my iOS application works fine, I can see records being saved and I can fetch them normally.
You are clearly looking at the wrong file. If your app is saving (I assume, across app restarts), the date is definitely saved.
One way to find out is to NSLog the persistent store URL and check the referenced file.
I believe the issue you are encountering is the new default journaling mode that Core Data uses.
See this article
https://developer.apple.com/library/ios/qa/qa1809/_index.html
Basically your changes are not written to the .sqlite file, but are found in .sqlite-wal. HOWEVER, most SQLite reading apps I have tried blow away the -wal when you open the .sqlite, so good luck.
You could try changing the journaling mode for debugging purposes

No data in tables when copying sqlite database to ios app

I'm using core data and am pre-populating it using a SQlite database that I provide in the bundle. It's been working fine for a while, however I've just edited the database and when I copy it over there is no data displayed. So I opened the database that is on the ios simulator, in base, and I found an sqlitemaster table that contains lots of SQL statements such as CREATE TABLE..., but the tables I want don't contain any data.
What's weirder is that after a while the database is fine and all the data is in it as normal. Is this because the statements in the sqlitemaster table were executed?
Has this happened to anybody else? I don't know whether it's because I've upgraded to iOS 7 or not? If anybody could point me in the right direction that would be great
The latest iOS Core Data SQLite backend uses journaling. You should notice .shm and .wal files in the same directory as what you assumed was your main sqlite backing store for Core Data. This data eventually gets into the main file, but journaling optimizes some situations for Core Data configured with SQLite.
As a developer, you generally never care about this from within the app, but if you are trying to inspect the persisted Core Data graph via the actual file, you'll need to be aware.
I've had a look around and the journaling was causing it to go all weird. Found this post which shows the same problem I had. If anyone else has this look at that post

Lightweight migration of existing core data database with iCloud to iOS 7

The WW2013 video on Core Data & iCloud mentioned that pre-iOS7 core data storage with iCloud can be migrated to the iOS7 way by specifying the NSPersistentStoreUbiquitousContentURLKey when setting up your persistent store coordinator.
Has anyone had any luck with this? With my persistent store, I had the SQLLite database in a .nosync folder and my log files in a different subdirectory. I've tried setting the NSPersistentStoreUbiquitousContentURLKey to point to each and I always get my entire database being over-written instead of everything migrating over.
I don't recall anything being said about migrating to iOS 7, I recall them indicating that to maintain compatibility with legacy apps where a custom path was specified for transaction logs you can continue using the NSPersistentStoreUbiquitousContentURLKey. This key should only be used to point to the log directory and nothing gets migrated when you use this, Core Data just uses the existing store and iCloud transaction logs.
To migrate it so that it uses the new defaults in iOS7 you would need to use the migratePersistentStore API to create a new store using a new file URL and only the NSPersistentStoreUbiquitousContentNameKey. Core Data will then automatically create the fallback and local (iCloud synced) store and iCloud transaction log files for you.
EDIT:
If anyone else is trying this an having problems try setting the store to use JOURNAL mode rather than the new default WAL mode. There seem to be some issues when doing certain migrations while using WAL mode. If anyone has figured out whether WAL mode has specific bugs please add a link here.

Core Data duplicate project for update of a submitted app

i have submitted an app on the app store that uses core data.
The problem is that i duplicated my project and then lost the first source code. If i update the app using the "duplicated" source code does it delete all core datas users may have stored? i know that if you change the file file-xcdatamodel without mapping the previous datas you will lose all you had stored, can i assume the duplicated project use the exact same file?
thanks in advance
In that case I think that, if you didn't modify your xcdatamodel, the data stored by user will keep during the upgrade. In order to be sure, you can install your own apo from App Store, save some data, update this app from Xcode with your new version and check if the stored data still are there.
If you've modify your xcdatamodel, you must perform a light or a heavy migration, depending on your canges.

Resources