QuickToDo CoreData + Rx + MVVM

Part 1: Core Data

Bratislav Ljubisic
2 min readJun 15, 2019

I have already started writing about new version of the QuickToDo app but, with all things happening in private life, moving to Germany, starting new job and trying to accommodate all of that in the same time, I had not much time left to work on this. Fortunately I have created substantial part of the app already. I will write more about those parts of the application: MVVM and RxSwift integration, including CoreData modifications.

First of all I have decided to use RxSwift implementation of frp paradigm. With that came the decision to use MVVM as way to organise the code. I will try to explain in details how I use those two paradigms together. But, before all of that I would like to write more about CoreData modifications.

First of all, there is an understanding that Rx should be used in Model section of app and as such, it is not directly visible in View section. Connection points between Model and View is ModelView which is subscribed to signals coming from Model and translate those to regular output variables consumable by View. Also, there is an understanding that View will never directly converse with Model, the connection should only be through ViewModel.

This is very simplified architecture sketch for the app. I will first try to describe the data model for the app. Whole source code can be found here.

Item object

As you can see here, Item is the focal point for QuickToDo app. Extension is added just to keep both initialisers for struct, one with variables and one empty, which will be used to create Item with default values. All properties of the structure are read only, and this was done on purpose, because I am using lenses to do updates to Item, as displayed here:

Lens definition

I would like to go into more details of the Core Data part. The most important parts are hidden in ManagedObjectTypeProtocol.swift file within CoreData folder. Here is the code explained:

Managed Object Type Protocol

Now, this ManagedObjectType is actually used when defining real Item Managed Object:

You can see that by using ManagedObjectType, you now have fetchInContext and findOrCreateInContext methods. Right now they are simple, but this is the place where you can add caching and all other fancy stuff from Core Data. Using the CoreData like this, you are using typed Entity and not generic Entity created using the String denominator. This change would prompt compiler errors if used incorrectly. Otherwise, if for example entity name is misspelled compiler would not register that error, but it will be present in run time. With all of these changes, it is very straight forward to write CoreDataModel.swift which can be seen in GitHub.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Bratislav Ljubisic
Bratislav Ljubisic

Written by Bratislav Ljubisic

Father of two and iOS developer in spare time

No responses yet

Write a response