Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Coredata 3
  • Overview
  • Transcript

2.5 Relationships Between Models

Hi and welcome back to Get Started with Core Data. In this lesson we will hook up the QuestionViewController and move the order of the questions around. I have prepared the classes between lessons and pasted what we learned in the last lesson already. They are almost done. And they're only missing a few lines of code in strategic spots. Let's start in the QuizTableViewController and set up the QuestionsTableViewController with the required properties. So if we are preparing the correct segue, we need to cast it to the specific controller class and set the quiz property on it. By using the FetchedResultsController and the index path, let us select that row. The QuestionViewController has another FetchedResultsController. I could also have just referenced the questions of the quiz object. Why did I do it the way I did? We have a lot of moving parts in this part of the application. Questions get edit, edited and updated. It is much more appropriate to use the FetchedResultsController. I also wanted to show the limitations of those. The next thing I want to hook up is the CreateQuestionViewController. Here I have to add the quiz property for starters. When we create a new question in the context, I want to reference the quiz afterwards as well. This is important since we have required an association with a quiz and a data model. Now we need to pass the quiz to the controller during the segue preparation. Here I formatted the function, not so generic as before but cast all the view controllers to the real class directly. While we are here, we also need to set the quiz on the QuestionDetailViewController. Right now, we are fetching all the questions there are in the database. And don't restrict them to the quiz. I'm going to add a predicate without much explanation right now, since we have a whole lesson dedicated to predicate. They're limited to the quiz object. Let's run the application and see what we've got. You can already see some questions I have pre-populated for a quiz. When we add a new question with an answer, you can see it gets added to the top. What we want to accomplish now is to make reordering work. There is a function called move row at index path. We can use for that, I'm going to implement this in a very naive way for now. I'm going to get all questions from the FetchedResultsController, remove the question we are going to move from it and insert it at the new location. Then I'm going to set this array as an ordered set on the quiz. Finally, we need to save the managed object context. When we run it and reorder the questions, we don't get an error. When we restart the application, though, it is a different order than what we set. This is a limitation of FetchedResultsControllers. Even though you have an ordered relationship, the FetchedResultsController can't be told to use this order when fetching since it isn't directly fetching a relationship but all entities. Filtering and sorting them with predicates and sort descriptors. It isn't all bad though because we can use this issue or bug to learn about migrations when we add another attribute to sort the values with fetch requests in the lesson about data migration. In the next lesson, we are going to have a detailed look at different data types and transformable attributes. See you there.

Back to the top