I've just been handed over an app with pretty vague instructions of how to update an internal sqlite DB.
In the past, I've done this with Cordova by just replacing an existing sqlite db into /www/ dir.
I also created a SwiftUI project using FMDB. I believe the code itself would automatically create a non-existing DB in app's Documents Directory. My code to modify the DB would then just modify this same DB in that same location. I never formally replaced the DB itself.
Anyways, instructions given to me for this app are to simply replace the DB (just a copy and paste of the .sqlite file) into the Project's root directory. This is literally just using Finder and copying and pasting. However, I don't seem to be getting the new data.
Should I be doing this in Xcode? I.e. 'Adding new file by right-clicking. Should I then copy file to project and/or add to target?'
I'm not sure this 'copy from Finder to project dir' is actually right. Quick glance at code shows that it is attempting to read DB from app's documents directory.
Is there a way to actually copy the DB to the app's documents directory?
Note: I noticed while building the app that it says something like "Copying db.sqlite to app". So that indicates to me that perhaps there's some kind of build rule that copies from the project Dir to the app's Documents Directory.
Any insights would be appreciated.
Thanks!
You need to add your DB to your project's structure by dragging it or File->AddFiles with selecting copy to project target
After that in your code you can read it like this
let db = Bundle.main.url(forResource: "File", withExtension: "sqlite")
And since you can't do any update operations to main bundle then you should copy this file to your document's folder the first time app launches , then read it from there any upcoming launch
Related
I wanted to know, if its possible to change the dependency of some directories.
To be specific:
In the app there are several information/pictures stored in an own-created (from the app after installing) directory called "Library". I cant access this directory from an unjailbroken device. But there is a directory called "Documents" which can be accessed from iTunes.
I want to change the dependency or whatever, so that I can grap or put on files in the "Documents" folder and the app will take them as well from there.
I tried to unzip the file and I had a look into some files but none of them contained a path to those directories. The only file remained was (I guess) the executable compiled file, which I could not encrypt/open/change.
Sorry for bad description - Im not sure how to depict it better.
I have an app in the app store that uses Core Data and I have to release an update by tomorrow. However, I am experiencing some problems with Core Data.I by mistake made changes to my model in Core Data and now my app is crashing. I tried migrating the data but the app still crashes when I updated it on my iPhone. Is there anyway to fix it?
Thank you so much for your help!
EDIT: I am trying to add the .mom file to the app but I can not get it into the Bundle:
EDIT 2: Do I delete the entirely Planner.xcdatamodeld:
You can recover the original Core Data model from the production app's bundle.
Use Finder to open the app bundle (Show Package Contents) and look for a .mom file or a .momd directory.
Copy the file or directory to some location outside the bundle.
Create a new XCode project with Core Data.
In XCode select the newly created projects Core Data model.
Go to the XCode Editor -> Import menu and select the .mom file you have just copied, if need be find the correct version in the .momd directory.
You should now have the model in XCode, save and copy the model file to you original project...
In XCode delete the incorrect model from your project (remember to save a copy first so you have a copy of the new changes)
Use the XCode Add File to... menu to add the old model to the original project
Now compile and run the app to confirm it opens the old file correctly
Now add a new model version and add the changes to this new version - make sure you select the new model version in XCode before making changes
You should revert your model to the one that you have in App Store. Then you should Add new model version of your Core Data model. If you can't revert to that model you should try to make it exactly as it was.
After you've done a new model version, you should select that one and make the changes.
If you don't use Git (which is really, really bad, by the way), you can just install your app from the App Store and use some iPhone File Manager to get the old mom file from your app. It would be in /apps/yorapp/youapp.app/ folder with .mom extension
Try iExplorer, for example, it's free.
I'm toying a lot with core data lately, and what bugs me is that you create a .xcdatamodeld to create your base structure, but then xcode convert it in a .momd file
My question is this : is it possible to do the conversion programmatically, instead of having it done by xcode?
Let's say for instance we wan't to be able to download a xcdatamodeld file from internet in an iOS app, and have it interpret it and create the underlying base.
You can create a momd file starting from an xcdatamodeld source using the momc script from Apple.
An xcdatamodeld “source” directory is compiled into a momd deployment directory, and an xcdatamodel “source” file is compiled into a mom deployment file.
(As explained here https://developer.apple.com/library/mac/documentation/cocoa/conceptual/coredata/articles/cdUsingMOM.html)
How to use the script:
Make a copy of the xcdatamodeld file you want to transform. It will be replaced.
Open the terminal and go to the Developer bin folder
cd /Applications/Xcode.app/Contents/Developer/usr/bin/
Run the momc script on your xcdatamodeld file
Usage: momc source destination
Example (it's important to put extensions):
./momc input.xcdatamodeld result.momd
If you download the entire xcdatamodeld bundle (it's a directory) and unpack it, you can use code from my momcom project to compile it into a .momd. You could then load that like any other data model. This project is still somewhat experimental, but it's performed well in testing so far. [Update: the project won't work on iOS as it is, because it uses NSXMLDocument. If you wanted to try this, you could use TouchXML, which is designed to be a drop-in replacement for NSXMLDocument. I don't know of any other options for compiling a Core Data model in your own app.]
The usual rules about having a data model and a persistent store match up apply. You couldn't just take the newly compiled model and use it with an existing store unless you handled model migration. Also, I have no idea what Apple's attitude would be regarding an app that downloaded new data model files-- it might be fine, or it might get you into some trouble with them.
No you can't download a xcdatamodeld file from internet and put it working. If you do so your programme will crash because of wrong data model. your data model needs to be in the main bundle and you have to select current data model. Core data maintain model version and some information about your data model. Even if you change your model and do not migrate the model your programme will crash and you have to delete the application from your device/simulator.
However, you can download the .sqlite database but data model should be the same.
I am using sqlite in my IOS project. I am copying the sqlite file from my main bundle to documents directory on launch of the application. It does copy, but suppose if I reset the simulator, the sqlite file from documents directory is also getting deleted, thus all the changes made goes away with it.
Any help would be appreciated.
First, you can readup here on where to save persistent data, and how to prevent iCloud backups. In your case of the simulator, you need to add code to your program wrapped in an 'ifdef SIMULATOR' (I don't have a reference handy on what define to test against, but I know there is one), and save a copy of your file to a fully qualified path that leads to your user account (ie /Users/xyz/iOS/xzy.data). When your app starts up, you first look for the file in the appropriate place as you would on the device. Then, if the file is not found, for the Simulator only, you see if there is a file in that hard coded path, and if so copy it back to the Simulator.
I have a project that uses Core Data.
When the app runs for the first time, do I have to copy the model to the Documents folder, so that the user can make changes?
Thanks,
RL
possibly.
if you are starting out with a pre-existing dataset, you can include the default dataset in the app bundle, and copy it to the document directory, where it will be writable.
however if it is starting as an empty store, it will automatically be created in the right place in the current Xcode templates.