Wednesday, June 19, 2024
HomeiOS Developmentios - Insert SwiftData merchandise with AppIntents

ios – Insert SwiftData merchandise with AppIntents


When utilizing App Intents, I can edit already current SwiftData objects, however I can’t insert new ones. Under are the 2 app intents I’ve used, each operate of their entirety aside from the mannequin context insertion on the finish of the second.

struct LinkViewedIntent: AppIntent {
    static var title: LocalizedStringResource = "Mark Hyperlink Seen"
    
    @Parameter(title: "Hyperlink")
    var hyperlink: LinkEntity?
    
    init(hyperlink: Hyperlink) {
        self.hyperlink = LinkEntity(hyperlink: hyperlink)
    }
    
    init() {}
    
    func carry out() async throws -> some IntentResult & ProvidesDialog {
        let entities = strive await LinkEntityQuery().suggestedEntities().filter({$0.considered == false})
        guard !entities.isEmpty else {
            return .consequence(dialog: "There aren't any unviewed hyperlinks to mark.")
        }
        var enteredLink: LinkEntity
        if let hyperlink = hyperlink {
            enteredLink = hyperlink
        } else {
            enteredLink = strive await $hyperlink.requestDisambiguation(
                amongst: LinkEntityQuery().suggestedEntities(),
                dialog: "Which hyperlink would you wish to mark considered?"
            )
        }
        let context = ModelContext(ConfigureModelContainer())
        let hyperlinks = strive? context.fetch(FetchDescriptor<Hyperlink>())
        guard let hyperlink = hyperlinks?.filter({ $0.id == enteredLink.id }).first else {
            return .consequence(dialog: "An Error Occured")
        }
        if hyperlink.considered == true {
            return .consequence(dialog: "Hyperlink is already considered")
        }
        hyperlink.considered = true
        strive context.save()
        
        return .consequence(dialog: "Okay, (enteredLink.title ?? enteredLink.hyperlink) has been marked as considered.")
    }
    
    static var parameterSummary: some ParameterSummary {
            Abstract("Mark (.$hyperlink) as considered.")
    }
}
struct SaveLinkIntent: AppIntent {
    static var title: LocalizedStringResource = "Save Hyperlink"
    
    @Parameter(title: "URL")
    var url: URL?
    
    func carry out() async throws -> some IntentResult & ProvidesDialog {
        let modelContext = ModelContext(ConfigureModelContainer())
        var fullurl: URL
        if let url = url {
            fullurl = url
        } else {
            fullurl = strive await $url.requestValue()
        }
        print(fullurl.absoluteString)
        guard let hyperlink = await makeLink(tackle: fullurl.absoluteString) else {
            return .consequence(dialog: "")
        }
        modelContext.insert(hyperlink)
        return .consequence(dialog: "I've added (hyperlink.metadata?.title ?? hyperlink.tackle) to Memento")
        
    }
}

The primary one works utterly as anticipated, the second makes the hyperlink object and sends the proper dialog on the finish however after I examine again within the app, the hyperlink hasn’t been added.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments