Fetching, updating and saving a record using Core Data - swift

This question has been asked a few times on stack but I am not finding a solution for my particular situation and I have been stuck for a few hours now.
My question is: How do I Fetch, Update and Save a record Using Core Data?
edit: This question has been modified to fit the title, which is what influenced the only answer that was received. Since Stack Overflow advises against deleting posts so I decided to revise this one to benefit future readers.

Documentation to create a new core data entity in swift.
Documentation to save an NSManagedObjectContext in swift.
To change the value of settingsSwitch simply change it to what it is not... so add that code to your didChangeSwitchState function.

Related

Append an object in openstack swift

Is it possible to append an already existing file/object in open stack swift.
If Yes, which version of swift supports it.
Also is it possible to boost the read/write performance in swift.
The answer to your first question is no. You cannot append to an existing object in Object Storage. You can only replace the object that is there.
The answer to your second question is too broad and vague to be answered in this forum.

Updating Database from iOS app [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Hi I am newbie to iOS programming,
I am building an iOS app to display a catalogue(Product name, Item No, Discription, and Image) of the products. After the user installs the app on the iOS device there may be updates happening to the product list. The user will not be able to modify any data in the database.
Can some one give me an idea of what kind Database i would require to use (SQL lite, Json or Coredata) and how i can let the update happen. Should I update just the new / modified records or update the complete database each time.
From some examples of apps i have seen from the appstore the app downloads the entire (latest version) of the database the first time the app is loaded.
Thanks in advance to all the friends out there in the community. your suggestions, codes, examples and any reference materials and links will be of great help.
Cheers!!
A couple of thoughts:
Apple's recommended framework is Core Data. Especially if you have a lot of data, it's probably worth familiarizing yourself with it.
Direct SQLite programming can have its advantages, but unless you have a compelling reason to pursue it (and I don't see anything suggesting this in your question), stick with Core Data. If you do decide to use SQLite, consider using the FMDB Objective-C wrapper for SQLite.
If you're dealing with a trivial amount of data (e.g. a dozen records), Core Data is probably overkill and you could just use a property list (plist). For example, if you have downloaded your JSON into a NSArray or NSDictionary, you can then just do writeToFile to save it, and dictionaryWithContentsOFFile or arrayWithContentsOfFile to read it back at a future date.
JSON is generally considered more of a mechanism for exchanging data with a server. I wouldn't be inclined to store data locally in JSON format (though you could). I'd use a plist instead.
By the way, it's generally not advised to store the images themselves in your CoreData/SQLite database. If you have larger files, for performance reasons, we often store them in the iOS File System (and save some reference to the file name in the database).
You mention that you have seen apps that download the entire (latest version) of a database. A more sophisticated implementation (critical with larger databases) would entail downloading updates (edits, deletions, insertions) rather than the full database. For a small database, if you can get away with the solution you propose (and it certainly makes it easier), but as your app becomes more sophisticated, you will want to consider more elegant server integration.

What is the use of Core Data and why we need it in iPhone development?

What is meant by Core Data in iPhone? Why we need it? What is the basic methods in it?
Thanks in Advance
It is essentially an ORM for the iPhone SDK, allowing you to define objects that act as a model which can be created, updated, and deleted through the use of OO and without writing SQL.
If you want more detail I would suggest accepting answers on your other posts like others have already suggested.

Images in iPhone app [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am creating a multilayered iPhone app using a navigation controller and the app includes a lot of images in every layer. I was wondering if it is a good idea to use a SQLite database to store all the images and extract them to the page when needed or should i just add all the images to the resources folder and pull them into the app as needed? I have used a SQLite database in an app before but the app was still slightly slow. Is there something else that I can do besides this to improve the speed of the app? I'm all ears, any help is appreciated. Thanks in advance.
If your images never change, there are some advantages you will gain from [UIImage imageNamed:], which is data caching. In your case, resource loading should also be faster than database access, because you access the resource directly instead of going through the database access layer. Caching also benefits you that you do not have to manage data caching by yourself, but you might have to release it from UIImageView when it is not visible to allow the library to do cache purging when needed.
I don't think you would get a faster response from SQLite than the file system. Either way it is stored in a file. I'm afraid that is the fastest you can do. That being said, you can make sure to load some files and views before they are needed if you have enough memory to do so. You can do this asynchronously while another part of the application is running using NSObject's performSelectorInBackground method. Also, you can reduce both load and processing times by optimizing the images' width and height beforehand for their target use. This makes a huge difference in both load and presentation time when you have a lot of images.
One thing to check is how long is it taking to query the database?
Check how long it takes from the query start until the results are returned. If this is slow, make sure you have indexed the column that you are using in your "where" statement.
You can quickly check with this awesome SQLite GUI
http://menial.co.uk/software/base/ and or add these.
I would get the data from the filesystem.

Best SQLite practices on the iPhone [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
What are some best practices to keep in mind when working extensively with SQLite on the iPhone? Tips/tricks/convenience factors all appreciated.
I can recommend using FMDB as a nice Cocoa SQLite wrapper.
Measure your app's memory footprint and look for leaks in Instruments. Then try it after invoking sqlite3_exec with:
pragma cache_size=1
and/or
pragma synchronous=0
YMMV. There are reports of performance boosts, large reductions in RAM usage, and fewer leaks. However, be careful about making adjustments without understanding the impact (for example, synchronous turns off flushing which speeds things up by a lot, but can cause DB corruption if the phone is power-cycled at the wrong time).
More here: http://www.sqlite.org/pragma.html
Off the top of my head:
Use Transactions.
Make sure your SQL leverages tables in the correct order.
Don't add indexes you're not entirely sure you need.
Perhaps not only specific to iPhone but to embedded devices there are some great tips here.
This link pertains to an older version of SQLite but still proves useful.
Lastly this Stack Question also has some good info.
We use SQLite with a .Net Compact Framework Application currently and it's performance is fantastic and we've spent a bit of time optimizing but not nearly as much as we could.
Best of luck.
I've found that it's often faster to just get the ID's I'm looking for in a complex query and then get the rest of the information on demand.
So for example:
SELECT person_id
FROM persons
WHERE (complex where clause)
and then as each person is being displayed I'll run
SELECT first_name, last_name, birth_date, ...
FROM persons
WHERE person_id = #person_id
I typically find this makes the complex query run in 1/2 the time and the lookups for a given person are typically on the order of 2ms (this is on tables with 17k rows).
Your experience may vary and you should time things yourself.
Also, I have to give credit to Wil Shipley for suggesting this technique in his talk here:
http://www.vimeo.com/4421498.
I actually use the hydration/dehydration pattern extensively from the sqlitebooks which is a superset of this technique.
I am lazy and like to stick in the core code as much as possible, hence I like the ORM tool SQLitePersistentObjects:
http://code.google.com/p/sqlitepersistentobjects/
You make your domain model objects inherit from SQLitePersistentObject (ok a little intrusive) and then you can persist/retrieve your objects as needed.
To persist:
[person save];
Loading it back in is almost as easy. Any persistable object gets dynamic class methods added to it to allow you to search. So, we could retrieve all the Person objects that had a last name of "Smith" like so:
NSArray *people = [PersistablePerson findByLastName:#"Smith"];
One other option I have not tried yet is Core Data (need to be an Apple iphone dev), although its a 3.0 feature and so it depends on your app whether thats an option..
PLDatabase is an FMDB alternative: http://code.google.com/p/pldatabase/
I've used it in one of my projects without issue.