High CPU Usage When Adding New Records in SwiftData

Hello,

I am experiencing high CPU usage and performance issues when adding new records (StudyLog) in my Swift-based app that uses SwiftData for data management. Here’s a summary of the situation:

  • Data structure: The app manages two main entities:

    • Subject (around 10 instances)
    • StudyLog (around 1000 instances), which has a relationship with Subject.
  • Issue: When I add a new StudyLog, I notice significant CPU rises to nearly 40 % for extended periods of time (sometimes longer than 10 minutes). While I don’t expect it to be instant, the current performance degradation seems abnormal, especially with this amount of data.

  • Environment:

    • SwiftData is being used to manage the models, and I am fetching all data using Query .
    • I am running the app on on both a simulator and a real device.
  • Models:

@Model
class Subject: Identifiable, Codable, Equatable, Hashable {
    @Attribute(.unique) var id: String
    var name: String
    var color: String
    var order: Int
    var doCount: Bool
    @Relationship(deleteRule: .cascade) var studyLogs: [StudyLog]?

    init(name: String, color: String, order: Int, doCount: Bool) {
        self.id = UUID().uuidString
        self.name = name
        self.color = color
        self.order = order
        self.doCount = doCount
    }
    //Equatable, Hashable, Codable, Transferable
}
@Model
class StudyLog: Identifiable, Hashable {
    var date: Date
    var hour: Int
    @Relationship var subject: Subject

    init(date: Date, hour: Int, subject: Subject){
        self.date = date
        self.hour = hour
        self.subject = subject
    }
    // Hashable
}
  • View:
struct ContentView: View {
    @Environment(Settings.self) var settings
    @Query private var studyLogs: [StudyLog]
    @Query private var subjects: [Subject]
    
    var body: some View {
    //body
    }
}

Key Questions:

  1. Are there any optimizations I should implement when dealing with SwiftData and relationships to improve performance when adding records?
  2. Is this level of CPU usage expected, or could I be missing something critical?

Any insights or suggestions on how to diagnose and resolve this would be greatly appreciated.

Thank you in advance!

@KFC_Japan

Welcome to the community.

What are the specifications of your Mac?

Thank you! Here are the specs of my Mac:

  • Model: MacBook Pro (2019)
  • Processor: 2.4 GHz quad-core Intel Core i5
  • Memory: 8GB
  • Storage: 512GB SSD
  • Graphics: Intel Iris Plus Graphics 655 1536 MB
  • macOS: 14.2.1(23C71)

Let me know if there’s anything else you’d like to know!

You will find that Intel chipped MacBook’s will suffer from high CPU load and you will also notice that it will get hot and the fans will run fast.

The M1 chipped Mac’s are way better. They run cooler, faster and it’s unlikely you will ever notice the fans running at all.

Unfortunately you will just have to put up with it while running a simulator. If you run the code on a real device, it will be fine.

1 Like

Thanks for the advice!

I’m glad to hear that running the code on a real device should be fine. I’ll definitely try it out on a real device and see how it performs.

Appreciate the help!