Model
ModelController
Provides functions to safely add and fetch objects from the persistent relational database in ‘Core Data’
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
ModelControlleradds 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 newMealsobject is added, the function searches for aDaylog with a matching day component of their dates, and adds the meals to that day’s meals relationship. If no matchingDaylog 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
ViewControllerGraphor 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
PersistenceServiceadds functions that allowModelControllerto find a shared context, make changes to objects within that context and then save it so that it persists on the device.
Fetching objects
ModelControlleradds 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.Mealsin 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,StressandDayis 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 moreDeclaration
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 moreDeclaration
Swift
class PersistenceService
-
Insulin NSManagedObject category/extension file
See moreNote
The following auto-generated properties are managed within ARISES.xcdatamodeld: fetchRequest(), time, units, dayDeclaration
Swift
class Insulin : NSManagedObject
-
Glucose NSManagedObject category/extension file: Contains a computed property
tagto mark if a log is in the hypo/hyper rangeSee moreNote
The following auto-generated properties are managed within ARISES.xcdatamodeld: fetchRequest(), time, value, dayDeclaration
Swift
class Glucose : NSManagedObject
-
Day NSManagedObject category/extension file: Contains many computed properties for summarily displaying related object information
See moreNote
The following auto-generated properties are managed within ARISES.xcdatamodeld: date, exercise, favourite, glucose, illness, insulin, meals, stress,Declaration
Swift
class Day : NSManagedObject
-
Favourites NSManagedObject category/extension file
See moreNote
The following auto-generated properties are managed within ARISES.xcdatamodeld: fetchRequest(), days, exercise, mealsDeclaration
Swift
class Favourites : NSManagedObject
-
Meals NSManagedObject category/extension file
See moreNote
The following auto-generated properties are managed within ARISES.xcdatamodeld: fetchRequest(), carbs, fat, name, protein, time, day, favouriteDeclaration
Swift
class Meals : NSManagedObject
-
Exercise NSManagedObject category/extension file
See moreNote
The following auto-generated properties are managed within ARISES.xcdatamodeld: fetchRequest(), duration, intensity, name, time, day, favouriteDeclaration
Swift
class Exercise : NSManagedObject
-
Illness NSManagedObject category/extension file: Contains a computed property
durationto compute duration of an illness logSee moreNote
The following auto-generated properties are managed within ARISES.xcdatamodeld: fetchRequest(), end, start, dayDeclaration
Swift
class Illness : NSManagedObject
-
Stress NSManagedObject category/extension file: Contains a computed property
durationto compute duration of a stress logSee moreNote
The following auto-generated properties are managed within ARISES.xcdatamodeld: fetchRequest(), end, start, dayDeclaration
Swift
class Stress : NSManagedObject
View on GitHub
Model Reference