Sorting with Core Data attribute - ios

I have a UITableView that is populated with table view cells from an NSFetchedResultsController. Each table view cell is linked to an entity object under Core Data.
I recently performed a lightweight migration and added an attribute that every entity object should have. The problem is, I am not sure how to apply it to the existing table view cells.
To be more specific, I want to add an orderingValue attribute to each object (task) that is linked to a table view cell. How would I make this orderingValue equal to the indexPath.row of the tableviewcell?

Related

NSFetchedResultsController per cell in a collectionView

I have a collectionView with NSFetchedResultsController. Some cells in the collectionView will have extra embedded UI elements queried from core data, and those extra embedded UI elements also need the update functionality of NSFetchedResultsController.
So my question is, what is the recommended way of approaching this?
Since the number of cells, and whether or not each of them has embedded UI elements depends on the data actually fetched from server, we cannot use sectionKeyPath of NSFRC right?
EDIT: the extra UI elements are not the same model as the embedding cells and thus require separate queries (NSPredicate).
EDIT: Our core data model:
RelationModel
type
status
Relationships(fromProfile, toProfile)
ProfileModel
..many fields
Relationships(photos)
Basically, the extra UI elements will be toProfiles with the embedding cell being the fromProfile. But because there are more than one kind of relations in the app, we decided to have a separate model for relations. And I found it hard to set a relationship from ProfileModel to the RelationModel
A NSFetchedResultsController is a really cool object. It does a fetch and then monitors core-data for changes. While it has an interface that relies on indexPaths so it is natural to think of these indexPath's as the same indexPaths as your collectionView there is no requirement that you do that. The indexPaths of the fetchedResultsController can be different than the indexPaths of the collectionView - you just need to careful about keeping track which indexPaths you are dealing with and translating from one to the other.
For example: You have a set of widgets that you fetching from core data. Some of the widgets have a property of extraWidgetInfo which you want to display in you UI as an extra cell. The fetchedResultsController says that there are 4 element (all in section 0). But the collectionView can display that as
[section1] widget1,
[section2] widget2, widget 2 extra info,
[section3] widget3,
[section4] widget4, widget 4 extra info.
While the fetchedResultsController only says that there are 4 elements, there are 6 cells in the collectionView. You would also have to translate the fetchedResultsController indexPath when dealing with updates. An update would translate to a reload section, and an add would translate to an insertSection and insert of some amount of rows in that section. You could also just call reloadData when core data updates (If you data is updating rarely this may not be a bad option).
Since we require separate queries in each cell, we ended up setting separate NSFetchedResultsController in each cell when it's dequeued. And then have the NSFetchedResultsController set as nil when prepareForReuse.

Tableview after tableview

I need to create two tableviews, a 'parent' and a 'child'. In the 'parent' tableview, the user can create a cell, give it a name, and upon clicking this cell, a second 'child' tableview will be displayed. In the 'child' tableview, the user should also be able to add unique cells to the cell that was tapped. For example, the user is presented with a table view. The user taps the + sign to add a cell, and then names it. The user taps Done and the first tableview appears again with the new named cell in it. The user taps the cell and then creates a cell that is unique to the cell above it. Using Core Data, how can I achieve this? I know how to create a simple tableview, but the part I'm having problems with is having the created cells in the second view controller be unique to where they were created.
It sounds like you need to create at least 2 entities in your core data model. I'll call the first entity Routine objects based on your comment. The Routine objects would have a relationship called "Tasks" with a destination to a Task entity. This would be a to-many relationship, so each Routine object could have many Tasks. When you select a cell, you would pass the managed object represented in that cell to the second table view controller so you can add Task objects to that specific entity.

What is the proper MVC design for a UITableView inside a UICollectionViewCell?

As the title describes, I've got a tableview inside each of my collection view cells. For me, it makes sense that the superview's controller should control the view, but since in this case each tableview contains different data, I have made each superview (collection view cell) the controller for its tableview. I hope that makes sense. I know making a view to also be a controller violates the MVC paradigm, but I'm not sure what the proper way is achieve MVC compliance in this case. I also need to send messages to the table view based on what happens in the CollectionViewController. Do I need to subclass UITableViewController and make a reference to it in my collectionviewcell.h file?
Sorry if that was confusing. Thanks for the help.
I think your instinct is correct that having a view object serve as a data source is a violation of MVC. I would suggest having the owning view controller either serve as the data source for all the table views, or set up a separate model object for each table view that serves up the cells for that table view.
If you use a single data source you'll have to have a switch statement that figures out which table view is asking and fills the cells with the appropriate data.
My gut would be to create a thin table view data source class who's only job is to serve up the cells for the table view inside a collection cell (and respond to the other collection view data source protocol methods). Use a custom subclass of UICollectionViewCell that has a strong property that points to the data source object. You could make your custom cell class create an empty data source object at init time and hook up it's outlet to the table view.
Then in your cellForItemAtIndexPath method, pass the appropriate data to the cell's data source object. If you reuse a cell, it would already have a data source object, so you'd just replace the data with new data and trigger the reloadData method.
Your controller object would mediate between the model and the view, like it should. It would set up the model data for each cell, and then the data source object for each cell would act as the model for that cells table view.
If you later come up with several different types of collection cells that display different data, using separate data source objects for each cell would keep the code simple. You'd just subclass your data source object based on the cell type.

Consolidate Table data into one

I have 4 different views, all that have their own table view with data. I want to make a 5th view/interface that has a table view that consolidates the other 4 tableview data into one.
For each of the 4 initial table views, I am storing the data using Core Data, and each table has its own entity with multiple attributes. Im looking for the theory about how to consolidate all these into one tableview. Any help would be great, or if you can point me in the right direction.
So essentially i have 4 table views all with their own data, and i want to make a 5th table view that shows all the data.
Thanks so much!
I guess what I would do is to store all the data of all the table views in a single mutable dictionary that contains sub dictionaries that contains each table view data like so:
MainDictionary >
---tableview1Dictionary
---tableview2Dictionary
---tableview3Dictionary
---tableview4Dictionary
I would save this dictionary on the NSUserDefaults so it can be accessed from each view controller in your app.
On the fifth view controller you can simply load this MainDictionary data in to your "combined" table view. This way whatever changes you make on any table view will directly effect your combined table view and all your data will be synced.
You can reload each table view on your controller viewDidAppear.
NSUserDefaults Tutorial: http://www.icodeblog.com/2008/10/03/iphone-programming-tutorial-savingretrieving-data-using-nsuserdefaults/
Hope that helps.

Order entities in Core Data data model

I am using Core Data in my application, and I have entities that need to be reordered. I have a collection view that is populated with NSManagedObjects from the data model, and this collection view can be reordered by dragging and dropping the cells. I want the order of the entities in the data model to reflect the order of the cells in the collection view. Does anyone know how I can accomplish this? I am new to Core Data, so I am still getting used to it.
If I understand your question correctly, you may do it simply as follows:
add an attribute viewOrder to your entity, probably an integer type
update it any time the user drags and drops a cell, i.e. re-orders the collection view (& make sure to save: the NSManagedObjectContext whenever you make any changes)
when populating the collection view, set the NSFetchRequest property sortDescriptors to sort the results by viewOrder
note that if you add new managed objects, you'll have to run a fetch to count the current number in core date or to find the max viewOrder

Resources