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.
Related
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
I've been working on an Xcode project on my desktop, and I was trying to save it to GitHub, so I could access it on the road. I've saved it to GitHub before, without issues, and the local Desktop version was working fine. After saving it to GitHub, I tried downloading it on the laptop, to make sure everything was fine, and one file was in red. It doesn't look like it was saved or even exists. I get a "Build Input File can't be found" error.
I went back to the Desktop version - that file is there and everything works. I thought, maybe I messed up with GitHub, so I tried to save it to iCloud, and then emailed myself a zip copy - same thing. Every other version, but the desktop version, had that one missing file in red.
I've looked at other posts for those with similar problems, and all the answers seem to say the same thing - the file was moved or deleted; however, it is still there on my original desktop working version. Any thoughts as to what is going on or what I should do are welcome.
I'm working on XCode 13.1, on a Mac running iOS 12.1
Follow up - locked files:
As some of you suggested, the file could be locked. It didn't appear so, and I thought it unlikely, as I don't know how to lock them. I copied the one file over, added it, and it is running, however, I got this warning when I tried to save and close it:
Locked file warning
I went through the steps to unlock it, and curiously, finder says it is not unlocked. Not locked but I'm getting the warning
Everyone has permission too
Any ideas as to why this might be? I'm stumped. I did try locking and then unlocking it, but no dice. I still get the error I don't have permission because it is a locked file.
Probably the file is not inside the project folder. This can easily happen with resources such as images and sound files that you copy to the project navigator. The project works fine but it has an external reference to the file. If the project folder is copied, eg zipped, the file is missing because it was not part of what was copied.
This generally occurs when you moved or deleted that file within Finder.
So try to delete that file from your project in Xcode and then re-add that file to your project folder in Xcode. Then your file would be working fine.
first: get your file from the current full path and cony it to any location on your mac enter image description here
Second: delete the red file from Xcode
third: drag file from the new location to Xcode don't forget to check on copy if you needed :)
On iOS, I save a file in /tmp, using NSTemporaryDirectory(). Can I access it with Xcode to see what was there ?
Note: Im not looking for NSLog for various reasons, I want to save to a file and retrieve this file with Xcode or some other tool, for debug. I don't care so much if the file is in /tmp or in another directory.
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 creating database in sqlite i create new database with constraint notnull and unique and save it to the place where is my application then i add things from my application and i check them in Library, Application support/.../..../... when i check database which is just created there then there this database is without Constraint and it accept same data.
why this is happening?
how can i make this database with Constraints just same like i have created while making application database
yes i have tried deleting database and making again
plz suggest something
One of two problems generally causes this sort of behavior (where you're updating a database and you're not seeing this propagated to the app):
The db in the bundle is not getting updated. You can check this by running app on simulator, opening bundle (go to the app in Finder, ~/Library/Application Support/iPhone Simulator/..., and select the app itself and choose "Show Package Contents"), and examining the db directly at that location. Bottom line, Xcode sometimes does not update the bundle properly for files altered outside of Xcode itself. This is fixed by selecting "Clean" from Xcode's "Product" menu, and then rebuilding and reinstalling the app.
The db that app is loading is not a copy of the version in the bundle. It doesn't sound like this is your problem, but many first time developers will copy the db from bundle to Documents folder if the file is not already there, neglecting to remember that when you install the app again, the contents of the Documents folder is preserved, and thus you'll still have the old version there. Just uninstall the app or reset the simulator if you're trying to address this for testing purposes.
Longer term, the app itself needs some logic that looks up the version number of the database (I create a "configuration" table with this information) in the Documents folder, compares that to what version the app expected to find, and if different, upgrade the database in the Documents folder.
Bottom line, it's useful to check all three databases (project directory, bundle, and app's Documents folder) to see at which step you lost your database changes.