TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Fetching SwiftData from AppIntentTimelineProvider does not work

Forums > SwiftUI

@amuu  

I am following the exact same way as Apple Backyard example app. the only diffrence is they fgenerate ing Daat on init of TimelineProvider mine is Fetching from context

struct WalletSnapshotTimelineProvider: AppIntentTimelineProvider {

    let modelContext = ModelContext(try! ModelContainer(for: Wallet.self, configurations:ModelConfiguration(isStoredInMemoryOnly: true)))

    func wallets(for configuration: PeekBalanceIntent) -> [Wallet] {
        if let id = configuration.specificWallet?.id {
            try! modelContext.fetch(
                FetchDescriptor<Wallet>(predicate: #Predicate { $0.uniqueID == id })
            )
        } else {
            try! modelContext.fetch(FetchDescriptor<Wallet>())
        }
    }

    func placeholder(in context: Context) -> WalletWidgetEntry {
        let wallets = try! modelContext.fetch(FetchDescriptor<Wallet>())
        return WalletWidgetEntry(
            date: .now,
            snapshot: WalletSnapshot(wallets: wallets)
        )
    }

    func snapshot(for configuration: PeekBalanceIntent, in context: Context) async -> WalletWidgetEntry {
        let wallets = wallets(for: configuration)
        return WalletWidgetEntry(date: .now, snapshot: WalletSnapshot(wallets: wallets))
    }

    func timeline(for configuration: PeekBalanceIntent, in context: Context) async -> Timeline<WalletWidgetEntry> {
        let wallets = wallets(for: configuration)

        guard let wallet = wallets.first else {
            return Timeline(
                entries: [.empty],
                policy: .never
            )
        }

        let snapshots = WalletWidgetEntry(date: .now, snapshot: WalletSnapshot(wallets: wallets))
        return Timeline(entries: [snapshots], policy: .atEnd)
    }

    func recommendations() -> [AppIntentRecommendation<PeekBalanceIntent>] {
        [
            AppIntentRecommendation(intent: PeekBalanceIntent(backyards: .all), description: "All Wallets"),
        ]
    }
}

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.