Post not yet marked as solved
Hello everyone!
I'm trying to run this project by Apple, but it doesn't let me, I'm trying to learn more about to swiftData...
https://developer.apple.com/documentation/coredata/adopting_swiftdata_for_a_core_data_app
On the code it says next:
`@MainActor #Preview {
AddBucketListItemView(trip: .preview)
.modelContainer(PreviewSampleData.container)
}
But it show me next error,
Type 'Trip' has no member 'preview'
anybody knows how can I solved, thank you.
Post not yet marked as solved
How secure storing the data using SwiftData? What encryption mechanism is it used to store data? Our project has a requirement to shore data using AES 256 encryption.
Post not yet marked as solved
What is the best way to use swift data to store one off data models? For example an application state that you want to be persisted across application launches.
The only examples I've seen use arrays of items which wouldn't work for having just one application state.
Is it possible to query just one item?
Post not yet marked as solved
I'm stuck at an error EXC_BREAKPOINT (code=1, subcode=0x1a8d69a38)that is thrown in a class during initialization.
The class is defined as:
@Model
public final class Parent {
@Attribute(.unique)
public var uuid: UUID
/// The date specification.
@Relationship(.cascade)
public var dateSpec: DateSpec
/// The title.
public var title: Title
/// The subtitle.
public var subTitle: Subtitle
public init(uuid: UUID,
dateSpec: DateSpec,
title: Title,
subTitle: Subtitle) {
self.uuid = uuid
self.dateSpec = dateSpec
self.title = title
self.subTitle = subTitle
}
}
The error is thrown in the var dateSpec property at the return self.getValue(for: \.dateSpec) call of the @PersistedProperty macro.
DateSpec is defined this way:
@Model
public final class DateSpec {
@Attribute(.unique)
public var uuid: UUID
/// The type of the date specification (`.point` or `.range`).
public var type: DateSpecType
@Relationship(.cascade)
public var point: DatePoint
@Relationship(.cascade)
public var range: DateRange
public init(uuid: UUID,
type: DateSpecType = .none,
point: DatePoint = .init(),
range: DateRange = .init()) {
self.uuid = uuid
self.type = type
self.point = point
self.range = range
}
}
And DatePoint is defined so:
@Model
public final class DatePoint {
@Attribute(.unique)
public var uuid: UUID
public var format: String
public var date: Date?
public init(uuid: UUID,
format: String,
date: Date? = nil) {
self.uuid = uuid
self.format = format
self.date = date
}
}
(DateRange accordingly).
So, as far as I understood the sessions, this should work. Or did I miss something?
-- Edit:
When taking out DatePoint and DateRange from the model, and replacing the properties by .transient wrappers that get/set the respective properties directly in DateSpec, then the error disappears.
So, is the problem the cascading of the relationships between Parent and DateSpec, and DateSpec and DatePoint/DateRange?
Post not yet marked as solved
I was wondering if ModelContext's fetch() contains an auto-updating results array? I ask because there is no documentation yet and in the WWDC23 lounge it was suggested by Debbie G to use fetch() to overcome the limitations of @Query i.e. no way to use dynamic filtering or sorting. I suppose we would call fetch() from onAppear and onChanged to support dynamic queries that also have auto-updating of results. Personally I don't understand why @Query and the old @FetchRequest were implemented as property wrappers instead of SwiftUI modifiers, e.g. .fetch(predicate:sort:) to match other modifiers like .task(id:).
I was hoping for it to behave similar to Date's formatted() that returns a locale-aware string that automatically invalidates SwiftUI Text when the locale changes. Although I'm not exactly sure how it works.
If fetch() doesn't auto-update then what would be the point of using it instead of just using NSFetchRequest with dictionary result type to get data into a SwiftUI view struct fast and memory efficient.
Post not yet marked as solved
I am trying to use SwiftData to perform a Query based on the name of the actor, which is inside a movie model. Here is the Movie model.
@Model
final class Movie {
var title: String
var year: Int
@Relationship(.noAction, inverse: \Actor.movies)
var actors: [Actor] = []
init(title: String, year: Int) {
self.title = title
self.year = year
}
}
Here is my actual Query:
_movies = Query(filter: #Predicate { $0.actors.contains(where: { $0.name.contains(actorName) }) })
But it returns nothing, even though I am passing actorName which exists in the movie.
I just saw the available video according SwiftData and wanted to convert a project of mine.
Both documentation and video mention this:
By default, SwiftData includes all noncomputed properties of a class as long as they use compatible types. The framework supports primitive types such as Bool, Int, and String, as well as complex value types such as structures, enumerations, and other value types that conform to the Codable protocol.
I tried to model an enum but I always get an error regarding the conformance to PersistentModel
I made different type of enum all implementing RawRepresentable but I always get an error
For example:
enum Simple: Int16 {
case one, two
}
@Model
class Item {
var simple: Simple = .one
}
I got those errors:
No exact matches in call to instance method 'getValue'
Candidate requires that 'Simple' conform to 'PersistentModel' (requirement specified as 'Value' : 'PersistentModel')
Candidate requires that 'Simple' conform to 'Sequence' (requirement specified as 'Value' : 'Sequence')
Did I miss something here?
Post not yet marked as solved
Hi,
I could probably wait until tomorrow's deep dive session and just find out, but I'm curious to ask, are the SwiftData persistent containers still backed on SQLite primarily?
I'm excited to replace Core Data in my existing SwiftUI app, but I've been dropping down to SQLite3 directly, for example, for using the FTS5 module for creating full-text-search tables alongside my Core Data tables to back more relevant results for .searchable UIs.