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

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.

Related

How to write custom accessors for an NSManagedObject subclass?

I have a view called Cart. It displays a table of Items. These Items need to persist, so Item subclasses NSManagedObject; values like id, price, etc. are properties whose accessors are #dynamic so that they are automagically generated.
I have another view called Favorites. It displays a table of Items, but they don't need to persist. In fact, this view changes whenever the user logs in with different credentials.
The connection between the two views is that the user can add items to his cart from his favorites. A cart can store Items from different Favorites lists. Favorites lists don't change when the item is added to the cart.
Initially, I made the model for the Favorites view be an NSArray of NSDictionary objects. When the user adds the item to his cart, I create and save the item in Core Data from the NSDictionary key-value pairs. This approach doesn't look very clean or very DRY. Wouldn't it make more sense to make the Favorites view's model be an NSArray of Items?
So now my intent is to implement the Item class so that it will represent the Core Data model (NSManagedObject), but also work with the Favorites view. Being new to Objective-C and iOS development, I really don't know how this would work or look like. It seems that I would need to override the accessors that are magically created for me, but I can't call them at compile time using a super call... Can anyone give me a rough outline of when it would know to return the NSDictionary data or the Core Data data? In the case that it's Core Data data, how do I maintain the same level of efficiency that the magically-generated accessors would have?
Even better, is there a better implementation that is just as DRY or makes more sense? Or am I trying to combine too much functionality into one class? Is the NSArray of NSDictionary objects the best way to go in this case?
You can specify a result type on your fetch request (object, objectID, count, dictionary).
Also, I would not use NSManagedObjects outside of a MOC. You should either have a separate object that you use for memory stuff, or you can use an in-memory persistent store for those objects... or, you can just create a separate MOC as a child to your main database MOC that you use for your in-memory objects.
The advantage of these approaches is that your code does not have to know whether they are backed to the disk or not.
As long as you do not save the MOC, the changes to those objects will never go to disk.
EDIT
NSFetchRequest *fetchRequest = // create the fetch request...
fetchRequest.resultType = NSDictionaryResultType;
Now, when you make the fetch, instead of getting back an array of NSManagedObject you will get back an array of NSDictionary.

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. ;-)

Referencing nsmutablearray to Data Model

I have a design issue which I am trying to analyze in my current project and thought maybe someone could help me figure this out. I have an nsarray object which I filter through predicate, and I want to set that object as my data model through view controller. First, is this a good practice of doing so? Since, I want to have an access of that object through out my transaction. I am not dealing with any database, plist, or core data model at current, these are just custom data model class I have created.
Thanks.
It's very common for a view of some sort to be backed by an NSArray, or an NSMutableArray. (Particularly, a UITableView, which can provide a single cell for each object in your array.)
Depending on the scope of your project, you can either investigate using Core Data for binding your model to your data:
CocoaDevCentral Core Data Overview
Or, for something a bit easier but less robust, you can look into implementing the methods defined in the UITableViewDelegate and UITableViewDataSource protocols, if you want to populate the cells of your table on a per-request basis.

Can you have a UItableView with mixed content (audio video text audio) from coredata?

I have a coreData model where I have an Event table and video, text, audio, image tables that have a many to one relationship with the event table.
In my app I have a rootTableViewController class that displays all the events. selecting an Event cell brings up a detailTableViewController that would display all the associated text, video, audio and image objects in the UItable. Objects should be sorted chronologically so there might be a mix of all the different object types in the table.
my question is: Is this possible to do this with a UItableView?
Is it better to fetch all the objects in a NSMutableArray, then sort the array and use it as an input for CellForRowAtIndexPath.
I was able to do this with a UIScrollView (without coredata). Inserting different views based on the object type; but i thought using a UITableView is better suited for data management.
Thanks a bunch for any help provided.
You generally only want to bother with fetches when you don't know which managed objects you are looking for. If you have a managed object but need other related objects, you just walk the relationship graph instead of fetching.
In this case the rootTableViewController had the Event object selected by the user. When it loads the detailTVC, it can pass it the Event object. The detailTVC can then ask the Event object for all its associated media objects. You can then sort them into an array anyway you wish.
To display each media type, create a custom tableview cell for each type and load that cell when the index points a managed object of that type.
An additional word of advice. This is wrong when you say:
I have a coreData model where I have a
Event table and video, text, audio,
image tables that have a many to one
relationship with the event table.
Core Data is not SQL. It does not have tables, rows, columns etc. Instead it has entities (abstract) and managed objects (concrete). Relationships connect entities in the model and managed objects in the actual live data. This is called an object graph and the main point of Core Data is to managed that graph. Unlike SQL, it really doesn't care how or even if the graph is ever persisted to disk.
Thinking of Core Data in terms of SQL always leads to grief.
What you could do is get the entities associated with a given Event with an NSFetchRequest and in the request, specify a sort descriptor for the date. Then, the array you get will be sorted the way you want.

iPhone: How do I retain the values after switching to another view and returning back to the same view?

How do I retain the values after switching to another view and returning back to the same view?
Is there a way to retain the array even after the view disappears?
My Array shows the values in console first time, but second time when it returns from other view the array shows (null)
EDIT:
I am using this array to use it into my core-plot.
This array contains the plot points for the core-plot
Generally speaking you have a few options to maintain data persistence between views. In no particular order:
Pass the array back and forth between the views. This involves creating a property on each view and setting it. You can use properties directly but you might have an easier time using NSNotifications to pass the data around.
Store the data in Core Data, loading lazily as needed. This is the most powerful option and may be a bit much. Core Data is not the correct way to "pass data between views", but often may be what you need. As an added bonus, your data stays saved across app launches.
Store the data in your delegate. This is similar to using NSNotofications. All of your objects can access the delegate more easily than passing references to each other all over the place.
Store the array in NSUserDefaults, assuming your data is compatible. This is similar to Core Data in that it offers persistence, but is much more "lightweight" and less powerful. Note that some objects are incompatible with NSUserDefaults.
Edit:
For storing an array of points, I would serialize it into either Core Data or NSUserDefaults, depending on how the user interacts with the data and how much data there actually is. If the points change often and there are many of them, look into Core Data. Although, you can do just fine with an array in NSUserDefaults. You just end up doing more array manipulation.