Model


ModelController

Provides functions to safely add and fetch objects from the persistent relational database in ‘Core Data’

Image of relational database graph

Image of relational database graph ARISES.xcdatamodeld

PersistenceService

Initialises a persistent store and provides functions to allow ModelController to add objects to its context and save that context.

Category/Extension files

One file for each NSManagedObject E.g. Glucose. Allows computed properties and methods to be added to objects in the database to extend their functionality, while automatically generating base property declarations from information added to the ARISES.xcdatamodeld relational database chart.

Features

  • Adding logs to relational database
  • Persistent storage of database logs
  • Fetching of filtered and sorted arrays of objects
  • Extending objects with computed properties to access key information easier

Adding to database

  • ModelController adds functions that search for existing objects with known links to the object to be added, then adds the new object as a relationship to that object. E.g. When a new Meals object is added, the function searches for a Day log with a matching day component of their dates, and adds the meals to that day’s meals relationship. If no matching Day log is found, one is created with the new date’s day component.
  • When objects are added to the database, they often post notifications, so that ViewControllerGraph or other classes can observe them and update their views accordingly.
let nc = NotificationCenter.default
nc.post(name: Notification.Name("FoodAdded"), object: nil)

Code for notification posted when food is added to database

Persistent storage

  • PersistenceService adds functions that allow ModelController to find a shared context, make changes to objects within that context and then save it so that it persists on the device.

Fetching objects

  • ModelController adds functions to fetch arrays of objects, primarily for use in creating tables of information. These arrays are filtered, and sorted in a logical order, depending on their contents. E.g. Meals in ascending time, Favourite meals in alphabetical order.

Extensions of objects

  • Many category/extension files contain computed properties which provide easy access to a summary information. These properties are used in many places. E.g. Day.foodStats, which returns a tuple containing the total carbs, protein and fat for that day, which is used to create the food domain status indicator.
  • The duration property visible in Illness, Stress and Day is working, but currently unused within the app

Future Work

  • Functions for removing/editing logs for use with future edit button functionality
  • Potentially refactor to use generic types to reduce boilerplate code
  • Adjust to work as a framework for use with continuous blood glucose monitor and activity band
  • Provides fuctions to safely add and fetch objects from the persistent relational database ‘Core Data’

    See more

    Declaration

    Swift

    class ModelController
  • Initialises a persistent store and provides functions to allow ModelController to add objects to it’s context and save that context.

    See more

    Declaration

    Swift

    class PersistenceService
  • Insulin NSManagedObject category/extension file

    Note

    The following auto-generated properties are managed within ARISES.xcdatamodeld: fetchRequest(), time, units, day
    See more

    Declaration

    Swift

    class Insulin : NSManagedObject
  • Glucose NSManagedObject category/extension file: Contains a computed property tag to mark if a log is in the hypo/hyper range

    Note

    The following auto-generated properties are managed within ARISES.xcdatamodeld: fetchRequest(), time, value, day
    See more

    Declaration

    Swift

    class Glucose : NSManagedObject
  • Day

    Day NSManagedObject category/extension file: Contains many computed properties for summarily displaying related object information

    Note

    The following auto-generated properties are managed within ARISES.xcdatamodeld: date, exercise, favourite, glucose, illness, insulin, meals, stress,
    See more

    Declaration

    Swift

    class Day : NSManagedObject
  • Favourites NSManagedObject category/extension file

    Note

    The following auto-generated properties are managed within ARISES.xcdatamodeld: fetchRequest(), days, exercise, meals
    See more

    Declaration

    Swift

    class Favourites : NSManagedObject
  • Meals NSManagedObject category/extension file

    Note

    The following auto-generated properties are managed within ARISES.xcdatamodeld: fetchRequest(), carbs, fat, name, protein, time, day, favourite
    See more

    Declaration

    Swift

    class Meals : NSManagedObject
  • Exercise NSManagedObject category/extension file

    Note

    The following auto-generated properties are managed within ARISES.xcdatamodeld: fetchRequest(), duration, intensity, name, time, day, favourite
    See more

    Declaration

    Swift

    class Exercise : NSManagedObject
  • Illness NSManagedObject category/extension file: Contains a computed property duration to compute duration of an illness log

    Note

    The following auto-generated properties are managed within ARISES.xcdatamodeld: fetchRequest(), end, start, day
    See more

    Declaration

    Swift

    class Illness : NSManagedObject
  • Stress NSManagedObject category/extension file: Contains a computed property duration to compute duration of a stress log

    Note

    The following auto-generated properties are managed within ARISES.xcdatamodeld: fetchRequest(), end, start, day
    See more

    Declaration

    Swift

    class Stress : NSManagedObject