MobileMakers Week 4 Day 2

Today we learned about CoreData and what maybe could best be described as its first layer. We were introduced to Entities and Attributes and how they work. We also reviewed how to save and remove Entities from CoreData.

We were told all morning that we have nothing to be scared about with CoreData. We learned that it’s complicated, we have to follow a particular order of doing things in order to make it work but once you get it to work, it is a very powerful tool.

What is CoreData?

CoreData works just like a database. It allows you to sort and filter large amounts of data based on textual queries. Its use is intended to make persisting and managing large amounts of data easy.

A good thing about CoreData is that if you create a new project by selecting CoreData, it will create all of the boilerplate code you will need. Although generally, you can ignore pretty much all the boilerplate code except for your ManagedObjectContext.

CoreData maps the data onto NSManagedObjects(A generic class that implements all the basic behavior required of a CoreData model object). All of the structure can be done in one single place which comes in handy. You manage all the structure in a xcdatamodel file. There you can add Entities, Attributes, Relationships, & Fetched Properties.

In a nutshell, Entities are your Classes and Attributes are your Properties.

We get data from our CorData model by using an NSFetchRequest. From here, you can further configure your data by sorting and filtering it even before asking our ManagedObjectContext to execute the query.

Finally, the way you add objects into your CoreData database by using a class method of [NSEntityDescription insertNewObjectForEntity:inManagedObjectContext] and set its properties.

HackWhich

Today’s lesson we completed what seemed a simple app at first but then as we began to see new things, it became somewhat complicated. For me particularly because my video stream kept freezing, even at the lowest resolution. It was so bad that by the time class ended, I was more than 30 minutes behind.

Today we created an app that basically added a tableview cell with a name that you enter and a random number between 1 and 10. The names were sorted alphabetically but had the option to filter cells by either having a number greater than or lower than 5.

After creating our file with CoreData, we went into our xcdatamodeld and set a new Entity named Warrior and had two Atrtributes, name and prowess.

Then we implemented four properties in our viewController. One NSManagedObjectContext, one NSArray, on BOOL and our tableview. In our viewDidLoad we created a new instance of our NSManagedObjectContext and assigned the managedObjectContext from our App Delegate.

We then loaded our data using an NSFetchRequest and implementing two NSSortDescriptors to handle the filtering. We then added our sort descriptors into an array and finally call our executerFetchRequest method. We then proceeded to implement our UITableViewCell and set it up.

The last step was to implement our action method that when the “Toggle” button is pressed, it will generate a random number and save it to our data model.

Here is the link in Github: TrowStoryFour

Challenge

Today’s challenge was great. I had a lot of fun and also banged my head against the desk for a couple of hours. It turns out that I need to remember to connect the delegates. Why I don’t do that in code instead, I don’t know.

Today we had to create an app that would display characters of Lost. The app needed be able to add a new character and edit an existing character. It also had to prepolute a plist before the first time it loaded and every time the character array was empty. Finally, the app needed to add a photo for each added character and of course enable the deletion of more than one row.

My Approach

The first thing I did was import the plist into my file. I then proceeded to create an NSManagedObjectContext property in my viewController and assigned it the managed Object Context from my App Delegate.

I then created a method that will handle the populating of the plist. In that method I created an instance of NSString that held the pathForResource to my plist. I then created an instance of NSArray and called initWithContentsOfFile and passed in the previous path. After that I created a for in loop and inserted a new object to my entity for every character in the plist. I finally wrapped all of that in an if statement that checked if my character array count was zero. If so then it will add the plist.

The next step was to fetch the daa from CoreData, I did this my creating another method and implementing my NSFetchRequest. I created a new instance of NSFetchRequest and called initWithEntityName, I then passed the name of my Entity in this case “Character”. I then called my character array and I executed my fetch request and finally added an NSSortDescriptor to sort my data by name.

I then proceeded to populate my tableview delegate methods as we have done many many times. This time however I implemented a method to change the name of the text shown on when you drag left to delete. I found that this method is super simple to implement and it only takes one line which is great! The method is titleForDeleteConfirmationButtonForRowAtIndexPath.

In order to add a new character, I needed to somehow save the information being inputed into the textFields as well as the image. I did this by setting an IBAction on a “done” button that would dismiss my modal view. I created a new instance of NSManagedObject and inserted a new object to my Character entity and setting the new values for each textfield and image.

Now, because my image was not a button but an UIImageView, and since they do not have any actions, I created an instance of UITapGestureRecognizer and when the image was tapped, I would implement my UIImagePickerController methods.

Two lessons for today: 1. Double check to connect your delegates and IBOutlets to storyboard and 2. Google is your best friend.

Here is the link in GitHub: LostCharacters

--

--

--

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

The Swift Summary in February 2022

Adding iPad support to your iOS app

Why a coding standard document is necessary for every team.

How to represent a list of different JSON objects using enums in Swift