@@ -17,6 +17,7 @@ associations, and more.
1717 * [ Dynamic queries] ( #Dynamic-queries )
1818 * [ Creating, update and delete data] ( #Creating-update-and-delete-data )
1919 * [ Associations] ( #Associations )
20+ * [ Booleans and enums] ( #Booleans-and-enums )
2021 * [ Migrations] ( #Migrations )
2122 * [ Lightweight migrations] ( #Lightweight-migrations )
2223 * [ Manual migrations] ( #Manual-migrations )
@@ -500,6 +501,91 @@ This style of handling associations does require you to be knowledgable in SQL t
500501correctly, but that is a benefit! SQL (and SQLite) are some of the most proven pieces of
501502technologies in the history of computers, and knowing how to wield their powers is a huge benefit.
502503
504+ ### Booleans and enums
505+
506+ While it may be hard to believe at first, SwiftData does not fully support boolean or enum values
507+ for fields of a model. Take for example this following model:
508+
509+ ``` swift
510+ @Model
511+ class Reminder {
512+ var isCompleted = false
513+ var priority: Priority?
514+ init (isCompleted : Bool = false , priority : Priority? = nil ) {
515+ self .isCompleted = isCompleted
516+ self .priority = priority
517+ }
518+
519+ enum Priority : Int , Codable {
520+ case low , medium , high
521+ }
522+ }
523+ ```
524+
525+ This model compiles just fine, but it very limited in what you can do with it. First, you cannot
526+ sort by the ` isCompleted ` column when constructing a ` @Query ` because ` Bool ` is not ` Comparable ` :
527+
528+ ``` swift
529+ @Query (sort: [SortDescriptor (\.isCompleted )])
530+ var reminders: [Reminder] // 🛑
531+ ```
532+
533+ There is no way to sort by boolean columns in SwiftData.
534+
535+ Further, you cannot filter by enum columns, such as selecting only high priority reminders:
536+
537+ ``` swift
538+ @Query (filter: #Predicate { $0 .priority == Priority.high })
539+ var highPriorityReminders: [Reminder]
540+ ```
541+
542+ This will compile just fine yet crash at runtime. The only way to make this code work is to greatly
543+ weaken your model by modeling both ` isCompleted ` _ and_ ` priority ` as integers:
544+
545+ ``` swift
546+ @Model
547+ class Reminder {
548+ var isCompleted = 0
549+ var priority: Int ?
550+ init (isCompleted : Int = 0 , priority : Int ? = nil ) {
551+ self .isCompleted = isCompleted
552+ self .priority = priority
553+ }
554+ }
555+
556+ @Query (
557+ filter: #Predicate { $0 .priority == 2 },
558+ sort: [SortDescriptor (\.isCompleted )]
559+ )
560+ var highPriorityReminders: [Reminder]
561+ ```
562+
563+ This will now work, but of course these fields can now hold over 9 quintillion possible values when
564+ only a few values are valid.
565+
566+ On the other hand, booleans and enums work just fine in SharingGRDB:
567+
568+ ``` swift
569+ @Table
570+ struct Reminder {
571+ var isCompleted = false
572+ var priority: Priority?
573+ enum Priority : Int , QueryBindable {
574+ case low , medium , high
575+ }
576+ }
577+
578+ @FetchAll (
579+ Reminder
580+ .where { $0 .priority == Priority.high }
581+ .order (by : \.isCompleted )
582+ )
583+ var reminders
584+ ```
585+
586+ This compiles and selects all high priority reminders ordered by their ` isCompleted ` state. You
587+ can even leave off the type annotation for ` reminders ` because it is inferred from the query.
588+
503589### Migrations
504590
505591[ grdb-migration-docs ] : https://swiftpackageindex.com/groue/grdb.swift/master/documentation/grdb/migrations
0 commit comments