I'm working on a project with movie information index. In this application when the manager updates a movie poster with referencing MovieID, it successfully updates the movie with filename of the uploaded jpeg image with extension as PosterSrc.
I used db.SaveChanges() to save the context after done editing the new values.
<img src="/UploadedFiles/Posters/#Model.PosterSrc" alt="#Model.Title" />
But in a different view, it does not render the updated image because it loads old value for the field of PosterSrc.
After updating value I made sure to check by both debugging and checking the updated value using a SQL query and could see it only updates database but not dbContext? What should I do?
Edit: It loads the updated field if I ran the application again.
Related
I am using sqlite in the first version of my app to store some data, now I have to change the structure for some tables.
Note that I released new version before and notice that the database was deleted after update since I did not see the database records after update and it start registering rows again.
My question is do I need to handle the tables deletion on update, and why apple delete the tables in the previous update?
It depends on the name of your database file, table name and the location.
From Apple's programming guide:
When a user downloads an app update, iTunes installs the update in a new app directory. It then moves the user’s data files from the old installation over to the new app directory before deleting the old installation. Files in the following directories are guaranteed to be preserved during the update process:
<Application_Home>/Documents
<Application_Home>/Library
Although files in other user directories may also be moved over, you should not rely on them being present after an update.
Reference: http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTuning/PerformanceTuning.html
There are several possible reasons why you did not see any data after the update. For example, the code in your second update could have changed the name of the database file, or the names of certain tables or fields.
In principle, the old sqlite store should be present after the update.
You will have to code the migration manually: on first launch after the update, create a new store, read every record from the old store and save it with the new format in the new store.
I have a part in my info.plist file that stores a key metric that the app uses to know how many db's it can expect to load. It is a simple number that I change as needed. For some reason, when the app updates from the App Store with new values, it is as if the updated plist values aren't there. The app is working based on the old values.
But if I remove the application completely, and then reinstall from the App Store, it works! What am I missing here? Why wouldn't the plist file be updating correctly with an update? Is there some sort of manual copy/update process I should be doing to the Documents directory or something?
Can you write code of how you are accessing data from info.plist.
Also, remember when you are storing any data in documents directory, it will not update until you make it forcefully.
And info.plist always gets update once you update you build.
Hope it helps....
My app is crashing when ever i change the data model. Inorder to run it again i should delete the app from simulator and the run it. Instead of doing this is there any means that we can run the app without deleting whenever we change the datamodel. i want the previous data to be used. Thanks in advance
The answer is a bit tricky but this always works for me. This is for a clean installation of a new compatible .sqlite file, not a migration!
launch simulator, delete the app and the data (the popup after you delete the app).
quit simulator
open X-Code, after making any edits to your data model
if needed update the datamodel version:
Editor > Add Model Version...
set your prefs in the dialog that appears (counting up is preferable)
click on the {appname}.xcdatamodeld then in the far right pane
click the left icon of the 3 icons on top of the far right column
under Versioned Core Data Model select the one you just created
delete the {*appname*}.sqlite file (or back it up, remove it from project folder, and delete reference)
clean the app (Product > Clean)
Run the app in a simulator (for this tutorial I will assume 4.2)
While the simulator is running, in a Finder window, navigate to:
{*home*} > Library > Application Support > iPhone Simulator > 4.2 > Applications > {*random identifier*} > Documents > {*appname*}.sqlite
Copy this file to another location
Stop running your app in X-Code
Drag and drop the {appname}.sqlite file into the files list in X-Code.
In the dialog that pops up, make sure the copy to folder checkbox, is checked.
Product > Clean
Then run the app in the simulator again
Now you should have a working sqlite file!
Cheers,
Robert
Basically you need to be able to migrate existing data to the new schema -- read up on Core Data Versioning and Data Migration.
The file being used for NSPersistentStore can only correspond to one version of a Data Model at a time. You need to either do a migration of the data to the new version or tell your application to delete the persistent store file each time you start (for development purposes only).
Just saw that you want to keep your old data. You can try serializing your data to a NSDictionary and then saving it to a plist/json/xml file. Then, when your program starts you can delete the old NSPersistantStore file and create a new one. Import the data from the plist/json/xml file to the new empty persistent store file.
Remember, in order for light migration to work you need to keep the previous version of the data model in addition to the new one. Core data needs to know both models, past and present, in order to perform a migration.
I have the same problem and I haven't fixed it yet. I don't care yet. While my app is in development I just clear the data every time I change the model.
I think to use Lightweight Migration, you still have to make a copy of your data model for every version of the data model you want to migrate from or to. It's lightweight, but not lightweight enough for when you're changing your data model frequently in early development.
I suggest you catch the exception it throws when it can't load the data, and have your program automatically delete the data in that case and recreate it in an initial state. It's the same as the ignore the problem answer but you don't have to manually delete the data every time. You probably ought to leave that code in for production, as a backup in case migration doesn't work for some reason, but maybe you ought to ask the user if they want to delete the data.
Here I've got a question concerning releasing update of the application in app store.
Suppose I've an application installed on my iPhone, which has some database inside, i.e. overtime user has entered info and the data were kept locally.
If the new version of application is released, and installed on my iPhone. Will the database be lost ?
I suppose all the information of the application, is removed and the update is installed like a new app. Please confirm.
Thanks
No the users data will not be lost.
When you update an app only the bundle data will be updated, meaning the .app directory of the installed app. Any other directory, like Documents and Library will not be touched.
If there is any data in for example the Documents directory that need updating then you have to write code to detect that and make the necessary changes.
If the database is used by Core Data then you will need to version and migrate the data.
All the files stored inside your app's documents directory (which is usually where db file is stored) are preserved during app update.
If you would have set the version number to your database for your iphone could have been easily handle, save your version number into your db and whenever database is called, compare the version against the expected version If new version > older version change the schema (this is needed if you would have changed the schema of your database) with using SQL ALTER statements and update the app version number.
so whenever user is going to update or fresh installation, it will check the new version with your older version, if it differ then update schema, and if its same no need to make any changes.
If you would not have made any schema related changes (for example adding new column..) then you do not need to worry, user will not lose the data.
my guess is that it would not be replacing any of the existing files instead just updating them if any changes....the best example of data is not lost is that.....i usually update most of my games. and they do preserve my highscore even after the updates.. lol! So, go on. Nothing will be lost. Nice question though. :)
I noticed several questions related to this topic go unanswered. Is this such a gray area that nobody really understands it?
Here is my problem:
I am a midway in the development of my app and the app has never been used ouside of my iphone simulator.One of the attributes in my core data structure requires a type change.Since my app has never been used outside of my iPhone Simulator, I first deleted the sqlite file. Doubling the effort of this step, I also went into iPhone Simulator menu and selected "Reset Content and Settings...".
Than, I edited the xcdatamodel file and changed the type of my attribute. I saved the file and exited. Without any other changes, I compiled. I expected it to fail because of my type change. It did not! After this, I assigned a value with new type to my attribute and it fails to compile?!
Is there something else that I need to do for the change to take an effect?
I would really, really appreciate an answer to my question.
Thank you!
Core Data sometimes acts weird until you do Build -> Clean to build from scratch.
When you change the model and there is no sqlite file then Core Data will just create one off of the current model, so your first instance makes perfect sense.
In your second instance, if you did not delete the sqlite file (or reset the sim) between those two iterations you would get an error because the sqlite file already exists from the last run and it no longer matches the model.
Whenever you change the model you need to either version it or delete the sqlite file. Otherwise they will not match and produce an error.
If that is not the issue then it would be very helpful if you gave the details of the error you are seeing.
update
I'd still like to know the right way of dealing with the changes in core data in early development stages when there should not be a need for the migration.
The right way is to delete the application / reset the simulator and start with a fresh sqlite file. There is no other option other than migration and as you surmised, that is incorrect during development.