Edit record un tableview using Core Data - iphone

I am new to core data .I have already performed insert and delete operations but a am not able to get how to perform Update (edit) in it. I am using a Tableview and on the detailview of it i want to edit my data which will reflect core data.
How can i do this ?

pass the selected NSManagedObject (or your subclass of it) to the DetailViewController and edit its properties.
If you have used NSFetchedResultsController and its delegate methods the main tableView should update when you return from the DetailViewController. If you haven't used them reload the table manually upon return (probably in MainViewControllers viewWillAppear: method).

Check out the examples given in Chapter 7 and 8 in below link:
http://www.headfirstlabs.com/books/hfiphonedev/
Then you only have to handle array in which you fetch records from database and then again write data into database.

Related

Reload collection view with new data

How would I reload new data, say an array of names (strings), without re initializing the collection view with data?
I know of [uiCollectionView reloadData], but I wanted to changed the array of information associated with the collection view.
reloadData causes the collection view to ask its dataSource about what it now contains. Whatever the dataSource uses to decide, this is your collection view source data. If you change that data then you need to call reloadData for your UI to get updated. You can change the data however you want (like adding / removing items from the array or creating an entirely new array).
You never initialise the collection view with data. You initialise it with a delegate and dataSource. So you aren't re-initialising it. You're just changing the data that the dataSource is providing to the collection view.

Alternative to NSFetchedResultsController?

Currently I have a UITableView with its data source being an NSFetchedResultsController. The most important thing the NSFetchedResultsController does is automatically update my table if there are any changes, via delegate methods. However, I no longer need to do a fetch to get my entity, call it "Pictures" for now. I have another entity called Folder, and folders have a relationship with Pictures, so every folder has an NSSet pictures.
So instead of fetching all pictures that belong to a certain folder, now I can just do folder.pictures, and that returns what I need, and I can assign that to an array and set that as my tableView source. However, this doesn't give me automatic table updates like an NSFetchedResultsController would.
My question is how can I have the functionality of an NSFetchedResultsController (that is, the delegate methods that automatically update my table) without executing a fetch? I don't need to fetch anymore since I have an NSSet with the desired NSManagedObjects.
What's wrong with the fetched results controller? Just keep it and use the dot notation for relationship sets as well - you get the best of both worlds.
The real advantage of the fetched results controller is actually hidden. It will fetch your objects (folders) alright - but maybe it will not fetch all the relationship attributes (pictures). This is called faulting. It means that core data will get the data in the background if it is needed. It is automatically optimized for speed and good memory usage. For example, the potentially huge array of your datasource will not have to be all in memory at once, something that is unavoidable with an array.
Thus, you really do not want to get rid of the FRC. She is your friend. Stay faithful to her. ;-)

Can I remove an object from the fetchedResultsController without deleting the object from the db?

I have been trying to remove an object from the tableView I am working with without deleting the object from the db, and I can't figure out how to do it.
I have tried setting a predicate on the fetchedResultsController, to filter objects that have a BOOL set to a certain value. Then, when I change that value, I expect that I can get that object to stay in the db, but out of the fetchedResultsController because of the predicate, but alas, that isn't working.
How can I remove an object from my tableView's dataSource (the fetchedResultsController) without deleting it from the core data db completely?
Please help! I've been bashing my head against this for way too long
I've always rolled my own table view data source when I need to weed out fetched results. NSFetchedResultsController objects are great when you want to show everything in your data base, but not so great when you want to weed out some of the data on the fly. I fetch the data, then iterate through the results array looking for the data I want to keep. The good objects get added to a new array, which becomes the basis of my table view data source.
The times I've implemented this, the data manipulation happens in a model object which hands off the array to the UITableViewController subclass that implements UITabelViewDataSource protocol methods. I suppose you could implement this as a subclass of NSFetchedResultsController, but I've never tried that approach.

Should I fetch all objects initially or when each view controller is loaded?

thanks right away. This is my first question and am excited to join the iOS developer community. I have one core data entity (say, a car). I have a tab view controller with two tabs - one displaying all cars and another displaying all types of cars (Chevy, Ford, etc.). The question deals with this second view controller. My question is - do I want to fetch all of my cars when the tab is loaded then pass all relevant cars of that type when the row is selected, or do I want to fetch my results after a row is selected meaning I'd have a different view controller for each type of car?
UPDATE:
I do, indeed, have two table views. The second one with the types has a list of types. When a row is selected I'm curious if I should pass the relevant cars to this VC or fetch the results?
I'd say you use 2 ViewControllers: 1 for all the Cars and then 1 for all the relevant cars of that type. You can just pass the relevant data (let's say an array of carmodels) from the FirstVC (where you put your initial array of cars , or dictionary if you will) to the secondVC and adjust your VC-looks accordingly.
(Seems you want to work with a UITableView to do this, it is very simple to pass these kind of messages through to a new VC and do the customization you want anyway)
You will all find your UITableView answers right here in the AppleDoc: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITableView_Class/Reference/Reference.html
A UITableView object must have an object that acts as a data source and an object that acts as a delegate; typically these objects are either the application delegate or, more frequently, a custom UITableViewController object. The data source must adopt the UITableViewDataSource protocol and the delegate must adopt the UITableViewDelegate protocol. The data source provides information that UITableView needs to construct tables and manages the data model when rows of a table are inserted, deleted, or reordered. The delegate provides the cells used by tables and performs other tasks, such as managing accessory views and selections.
One of these delegate methods you seek is the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This method will trigger when you select a row in your tableView. In this method you want to call and show your new VC and this is also when you pass the relevant data to that new VC. The rest of the above will still hold up then.
Good luck.

iPhone: NSFetchedResultsController for each Entity?

I have three entities with one-to-many relationships (Book <--->> Page <--->> Text)
I want to use one table view to present Book.titles, one table view for Page.no and one view to show the Text when clicking on a Page.no.
Do I need to setup up a fetchedResultsController for each entity or can I get access to a Text object
using the Book entity - Book.pages... etc?
If I understand your question correctly, you only need one fetched results controller and CoreData will do the rest.
So, you fill an NSFetchedResultsController with all the books you want to display and present them with a UITableViewController subclass. Then when the user selects one, you pass this book on to another UITableViewController subclass which uses book.pages to get and display all the pages in that book. This idea is then repeated to show the text entities.
HTH
PS - If you aren't already, you may find it useful to use xCode's Managed Object Class generator to ensure book.pages and pages.texts are correctly set up. To use this, open your .xcdatamodel file, highlight an entity and choose File->New File and choose Managed Object Class and then follow the steps.
You can access all objects via the relationships but depending on how complex your tableviews are, it may get tricky to manage the datasource methods. It's doable though!
For instance, if you decide to fetch the books, provided you have a reference to the instance you get all pages and put them in an Array with:
Book *bookItem = [self.frc objectAtIndexPath:indexPath];
NSArray *dataSource = [[book objectForKey:#"pages"] allObjets];
Another option is to create an abstract class and make it a parent of all 3 entities, then fetch using this class which will give you an array of all books, pages and text. From there you can test for the class and populate the tables accordingly.