Skip to content

Category: Backend

Order your Core Data entities the right way for maximum speed

One of the earliest things you learn about Core Data is that it doesn’t support ordered entities out of the box. Sometimes you can have an ordered relationship, but the minute you try and move that database to iCloud, you find that ordered relationships aren’t supported. Long story short: if you want to order your entities, then you need to create an index attribute that allows you to sort your entities yourself.

So you do the natural thing: create your index attribute as an Int, and then assign each in order: 1, 2, 3…

This is fine if the data doesn’t change, but if this is data the user can add to and reorder, what if they want to move element 3 between 1 and 2? It means you need to change 2 entities in addition to adding the new one. What if there are 500 entities, and the user wants to insert a new first element. 500 entities to change because the user moved a single entity?!

There must be a way to insert and reorder IDs that avoids all of this rewriting, right?

Thankfully there are a couple of techniques you can use that will almost completely eliminate the need to rewrite a huge number of entities every time you need to insert or rearrange entities. Let’s talk about them now.

Got JSON? Need objects? Stop typing, and start generating your object models with Quicktype

It’s time to integrate another API into your app.

That statement alone brings all these feelings to mind:

  • Poring over the API documentation, teasing out the JSON requests and responses for each call you need to use
  • Extracting a schema out of those requests and responses, being mindful of the structures, types, and required/optional state of each field
  • Writing the Swift or Objective-C code to translate those JSON schema into an object model

Hours gone in an instant. Isn’t parsing and translation something a computer should be taking care of? It can, using a tool called Quicktype.

4 ways to connect your app to a database, and 1 way not to

When you’re putting together a new app, especially if it’s your first exposure to iOS development, it can be a confusing experience. Sometimes you’re starting from scratch, and you have a bunch of options available to you, and you just don’t know which one is right for your app. You may have even been “helped” by a PaaS or IaaS platform that has given you a conventional SQL database instance: great for web apps, but how do I connect this to my iOS app? Do I want to?

There are many different ways to provide a database backend for your app. Here are some recommendations for different situations.