Skip to content

Conversation

@jcmourey
Copy link
Contributor

@jcmourey jcmourey commented Dec 5, 2025

And do not seed sample data to preserve the SeedSampleData UI.

… previews so lists will appear. And do not seed sample data to preserve the SeedSampleData UI.
#Conflicts:
#	Examples/Reminders/Schema.swift
#	Examples/RemindersTests/Internal.swift
@jcmourey jcmourey changed the title Bootstrap the database with a nil SyncEngineDelegate in RemindersList preview Bootstrap the database with a nil SyncEngineDelegate in RemindersList preview so lists will appear Dec 6, 2025
@jcmourey
Copy link
Contributor Author

I saw in your last couple of episodes that you seem to prefer making an explicit call to seed the data from preview rather than letting bootstrapDatabase handle it based on context. Would you prefer I modify this PR accordingly?

@mbrandonw
Copy link
Member

Hi @jcmourey, yeah actually that would be great. Thanks!

@jcmourey
Copy link
Contributor Author

jcmourey commented Dec 17, 2025

Running into a really odd problem. Preview code gets called repeatedly unnecessarily, but that's probably a Xcode problem and is normally not an issue.

When the data is seeded from within appDatabase, it's ok, because the database gets resets everytime appDatabase is called.

HOWEVER, when the data is seeded from the preview set, the database strangely doesn't get reset leading to conflicts on inserting tags (the only table without a ON CONFLICT REPLACE clause), even though the call to seed() is immediately after appDatabase()

VERSION 1

struct RemindersDetailPreview: PreviewProvider {
  static var previews: some View {
    let (remindersList, tag) = try! prepareDependencies {
      $0.defaultDatabase = try Reminders.appDatabase()
      try $0.defaultDatabase.seed()
      ...
    }

However, the problem goes away if I do this instead:

VERSION 2

 struct RemindersDetailPreview: PreviewProvider {
  static var previews: some View {
    let (remindersList, tag) = try! prepareDependencies {
      let database = try Reminders.appDatabase()
      try database.seed()
      $0.defaultDatabase = database
      ...
    }

Additionally, this problem only occurs inside PreviewProvider, not inside #Preview

I've been investigating, but I don't understand why VERSION 2 would behave differently from VERSION 1. Does this look familiar to you?

@jcmourey
Copy link
Contributor Author

jcmourey commented Dec 17, 2025

I worked around the problem using lazy evaluation of dependencies in preview. It's also faster because the database doesn't keep getting recreated 5-10 times for each preview.

Doesn't explain away the issue in my previous comment which is somewhat disturbing but at least it works.

…h preview as appropriate.

Moved to lazy initializers in previews as needed to avoid unnecessary multiple re-creations of the database that sometimes lead to seeding conflicts.
@jcmourey jcmourey changed the title Bootstrap the database with a nil SyncEngineDelegate in RemindersList preview so lists will appear Fix RemindersList preview so lists will appear and other preview matters Dec 17, 2025
@jcmourey
Copy link
Contributor Author

Just ran some performance benchmark on the RemindersDetail preview (not including the building time, just the computing/rendering time)

  1. preparing dependencies inside the preview body: the previews body is invoked 9 times, and so dependencies are prepared 9 times, including creating and seeding the database

Total time = 690ms

  1. preparing dependencies in a lazily-evaluated static variable: the previews body is invoked 9 times, but dependencies are prepared only once:

Total time = 320ms

Development platform: MacStudio M4 Max 128GB RAM, macOS Tahoe, Xcode 26.2

…he type name since it's not an actual migration). Only create the table if it wasn't already created
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants