Im trying to load sample date for view Previews in SwiftUI from a SwiftData model. I created an extension to the SwiftData model show below to load sample data for Previews as shown below. In the app imageData is stored in the SwiftData model by using the getDataFrom function I listed below as well which loads the image from URL, converts to Data and saves in SwiftData. I'm stuck on how to load imageData for a View Preview???
@Model class BookWithMatchingData: Identifiable, Codable, Hashable {
var author: String
var isbn13gd: String
var title: String
@Attribute(.externalStorage) var imageData: Data?
...
// create sample data for preview
extension BookWithMatchingData {
static var sample: [BookWithMatchingData] {
[
BookWithMatchingData(
isbn13gd: "9781538724736",
title: "Sample Title",
author: "Sample Author",
//imageData: ????
)
]
}
}
//
func getDataFrom(url: String) async -> Data? {
do {
let (data, _) = try await URLSession.shared.data(from: URL(string: url)!)
return data
} catch {
print("Error fetching data: \(error)")
return nil
}
}
Im trying to load sample date for view Previews in SwiftUI from a SwiftData model. I created an extension to the SwiftData model show below to load sample data for Previews as shown below. In the app imageData is stored in the SwiftData model by using the getDataFrom function I listed below as well which loads the image from URL, converts to Data and saves in SwiftData. I'm stuck on how to load imageData for a View Preview???
@Model class BookWithMatchingData: Identifiable, Codable, Hashable {
var author: String
var isbn13gd: String
var title: String
@Attribute(.externalStorage) var imageData: Data?
...
// create sample data for preview
extension BookWithMatchingData {
static var sample: [BookWithMatchingData] {
[
BookWithMatchingData(
isbn13gd: "9781538724736",
title: "Sample Title",
author: "Sample Author",
//imageData: ????
)
]
}
}
//
func getDataFrom(url: String) async -> Data? {
do {
let (data, _) = try await URLSession.shared.data(from: URL(string: url)!)
return data
} catch {
print("Error fetching data: \(error)")
return nil
}
}
Share
Improve this question
asked Mar 17 at 18:47
user1233894user1233894
1,7761 gold badge12 silver badges11 bronze badges
3
|
1 Answer
Reset to default 0Apple has provided PreviewModifier
so you can create and quickly reuse preview Context
struct SwiftDataPreview: PreviewModifier {
static func makeSharedContext() async throws -> Context {
// Mock/In-Memory container code
}
typealias Context = ModelContainer
func body(content: Content, context: Context) -> some View {
content
.modelContainer(context)
.task {
// create and insert sample models
}
}
}
extension PreviewTrait where T == Preview.ViewTraits {
@MainActor static var swiftDataNull: Self = .modifier(SwiftDataPreview())
}
Then you can use it anywhere
#Preview(traits: .swiftDataNull) { // <--- Here
ContentView()
}
@Model class
already conforms to Identifiable and Hashable, no need to do it again. There are a number of SO posts on how to use SwiftData with Previews, search for them. See also, hackingwithswift/quick-start/swiftdata/…. What have you tried? – workingdog support Ukraine Commented Mar 18 at 0:08