MobileMakers Week 4 Day 3

Today was day two on CoreData. We learned about how and when should we should form relationships between CoreData entities, there was a brief description about inverse relationships and how to instantiate a view controller from storyboard programatically.

CoreData makes it easy to control relationships between managed objects. The most fundamental decision that must be made before creating a relationship is also quite simple: is it a to-one or a to-many?

Implicity, “to-one” and “to-many” typically refer to “one-to-one” and “one-to-many” relationships.

You can define a many-to-many relationship using two to-many relationships. The first to-many relationship goes from the first entity to the second entity. The second to-many relationship goes from the second entity to the first entity.

Xcode also makes it easy to create cutom classes for your data model’s managed objects through the subclassing of your NSManagedObject class.

Hackwhich

I’m not going to talk about today’s hackwhich much mainly because we were given a partial completed project and we just went step by step filling in the blanks and touching briefly on every topic. The gist of it though was basically creating a many-to-many relationship between two Entities.

Challenge

For today’s challenge, we were shown a wire frame and we had to replicate the steps that the instructor wanted. This was a great experience because it gave us a glimpse of how clients are approached and how to work with other developers.

The app itself seemed relatively simple. It was a simple app where you could select friends out of a fetched son string, the people you selected then became your friends. The point of the app was to replicate a book club. In this case, the user could suggest books to the friend. You could add the book via text fields.

The app could also update the friends and book images respectively. Next, when you selected on a book that was recommeded to your friend, it would show you the details of the book and how many comments have been made on that particular book. Finally you could add a brand new comment to the book you selected.

Here is my finished project:

BookClup App for MobileMakers Week 4 Day 3.

My Approach

Ok well.. first things first. We were going to be interacting with a friend, a book and a comment. So the first thing I did was create three Entities. Every friend had an image, a name and wether he had been selected or not so I created three attributes for my Person Entity. My book had four things, an author, a genre, a title and a book image so I created four attributes for my Book Entity. Finally every comment had two things, the comment itself and the date when it was posted, so guess what I did, that’s right, created two attributes for my Comment Entity.

My Person entity had two relationships, one for friends and one for suggestions of books, both of these were To-Many relationships. My Book entity also had two relationships, one for comments and one for the person suggesting the book. These also were To-Many relationships. My Comment entity only had one relationship, the book that was commented on, this was a To-One relationship.

I then created five subclasses of UIViewController. One for each of my views. Ont he first view, we intiated the app with a blank tableview but it needed to have certain functionality for when we had some data in it. I made helper methods to make the fetch request to CoreData so that when we got some names in our tableview, they would appear. I then created a custom method that took in a BOOL as a parameter so that we can set our sorting to ascending.

I then proceeded to implement my tableview delegate methods and also search bar method. In my search bar method, I created an instance of NSPredicate I then assinged the filteredArray using the predicate to my filteredFriends array.

In my AddFriendViewController.h file, I added a property of NSManagedObjectContext type. In the implementation file, I implemented the tableview delegate methods. In my didSelectRowAtIndexPath, I created a new instance of Person and assigned it the objectAtIndex of my selected row. I then implemented an if statement to check if the selected row(or person) already in my friends array as selected, if so, it would unselect the friend and remove the object from my core data stack. If it was not selected, it would select it and add it to the stack. I finally saved my modelObjectContext file and reloaded the tableview.

In this file I also implemented an instance of NSFetchRequest and added two predicates to it. I basically wanted to formatted it in two ways, one if the name was equal to one of the my friends and one if it wasnt. This way to differentiate from the selected status with a checkmart. Also, if my friendArray was empty, it would then fetch the data from the url. In my data fetch method, it was a pretty standard NSJSONSerialization method call. The only difference is that I created an instance of my Person managed object and inserted a new object for ever person in the json file.

Moving on to my FriendDetailViewController, in the header file, I created a property of my Person managed object. In the implementation, I added a UITapGestureRecognizer to my user image, created an instace of Book in my cellForRowAtIndexPath and assinged it the indexPath of the row. Here I implemented my UIImagePickerController methods to obtain a new image when the image was pressed and then save it to my moc.

In my AddBookViewController header file, I created another property of Person. The same gist goes on here, create an instance of UITapGestureRecognizer, implement the UIImagePickerDelegate methods to get any pictures.

Finally, on my action for the “Add” button, I created a new instance of Book and assigned it to my entity description. I then passed a for in loop that basically set the core data values as the values in the textfield. I finally saved it to my moc and reload data.

Moving on to my last view controller. In the header view , I created a Book property. The only things different in this view controller, is that I overrid the fetchHeaderinSection and set the book’s image as the books background.

Finally, I created an action for the add button. In this method, I implemented an instance of UIAlertController and gave it two actions. I then added two buttons to my UIAlertController, one for cancel and one for confrimatino of the action.

Here is the link in Github: BookClub

--

--

--

iOS Dev, Tech junkie, Dad & Husband.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gabe Morales

Gabe Morales

iOS Dev, Tech junkie, Dad & Husband.

More from Medium

How to get started with Voice Over on iOS

An image of a microphone

So you want to do mock testing in XCTest?

Deploying apps to Apple’s B2B App Store.

The app store logo as displayed on an iPhone

How to add CoreHaptics to your iOS app