UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Help with designing a Core Data model

Forums > SwiftUI

Hey there,

I'm a beginner learning SwiftUI and working on my first app. I have a prototype working with placeholder data that I want to replace with Core Data.

It seems like the decisions I make now on the data model could have a big impact on the complexity and performance of my code later on. So I'm hoping folks with more experience would be willing to give me some feedback and direction.

The app is designed to help me maintain a daily meditation practice. It's a simple timer with a focus on keeping a streak alive. Here are screenshots of the relevant screens:

The data I need to display in the UI:

  • Current streak (number of consecutive days a meditation session has been logged)
  • Longest streak
  • Total number of sessions
  • Total days/hours/minutes spent meditating
  • Progress toward the next milestone. Milestones are 7, 30, 60, 90, 180, 365 days

My current draft contains one entity with two attributes:

MeditationSession entity

  • date : Date type (set to Derived with now() as the Expression)
  • duration : Date type (set to Use Scaler Type so that it becomes TimeInterval)
  • Should I add any other attributes to make my life easier? ID etc?

The parts that seem straightforward:

  • Get the Total Sessions by calculating the count of MeditationSession
  • Get the Total Days/Hours/Minutes by getting the sum of duration attribute

For streaks, I'm less confident about the best approach:

  • Use the date attribute on MeditationSession and calculate the consecutive days in a function? This seems pretty complicated for someone brand new to programming. Any APIs that I should look at using here to make my life easier?
  • Should streaks be added to the model?

Milestones, even less confident:

  • Do I need another entity in the model for this with boolean attributes for each milestone?

Many thanks in advance for any input you can provide!

3      

I haven't yet implemented streaks but I would probably go with UserDefaults since both current and longest streak are counts and individual pieces of data that do not fit well in traditional database.

As for streak calculation, you can check each day if streak continues and if so increment counter? If user launches the app and date of their last meditation is not today or yesterday, then you can reset the streak.

At the point of reset you would check the longest and update if necessary.

The milestones are for total number of meditation or longest streaks? Either way I would store these in Core Data (you can prepare function to prepopulate it on first launch). Then after each meditation completion, you can get total count, loop over incomplete milestones and mark completed as needed.

3      

@nemecek-filip thanks so much for taking the time to reply.

I like the idea of using UserDefaults for streaks but I plan to have Watch and Mac companion apps. Would I be able to access the streak counts across phone, watch, mac with UserDefaults?

How would you suggest I add Milestones to Core Data? As a new Milestone entity with each milestone as a boolean attribute? Create a User entity with milestones as attributes?

Thanks again!

3      

There are some way of synchronizing UserDefaults across devices but since you are going to have Core Data synced already in that case I would store it there.

I would prepare Milestone entity in this way:

class Milestone {
  @NSManaged var neededCount: Int
  @NSManaged var isAchieved: Bool
}

But since milestones can be initialized with just knowing how many meditation sessions the user has completed, you could generate the view on the fly. In this case you would have some constant array of milestones count and based on that + number of session already done you could display all the badges as either completed or in-progress.

3      

@nemecek-filip that makes sense -- thanks so much.

3      

I prefer to not try to fit things into places that aren't obviously for them User defaults has a meaning, and a streak is not a default, it is a calculated value.

If you have Core Data implimented, then that, IMHO, is where you model should reside. One stop shop so to speak.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.