Week 10: Pulling from Database

April 13, 2025

Thoughts of the week

This week, I have started to work on pulling the data from the MongoDB database and storing it locally on the device. This will ensure that if the user goes on and has a bad reception, the data stored on the device will be used instead of giving the user nothing. I want an automatic refresh later, so periodically, it will check for new data and update the local data if connection permits. I will have to be careful about this as if the user is in the middle of a workout, don't change anything until after they are actually done.

Local Data Storage Implementation

To store API data locally within my app:
  • I used UserDefaults for local storage
  • Apple offers other persistent storage options like CoreData and FileManager
  • I chose UserDefaults because:
  • It's simple to implement with minimal setup
  • Perfect for storing moderate amounts of data
  • Built into iOS without extra dependencies
  • Automatically handles data persistence between app launches
  • I stored the data by:
  • Converting API objects to Data using JSONEncoder
  • Saving the encoded data with unique keys in UserDefaults
  • Loading and decoding the data when needed using JSONDecoder
  • Tracking when data was last updated with a timestamp

Images

stored beacon data
Database data.

Next Steps

Implement User UI and store their personal workouts.