Unable to Save Intents

Hi All, I'm using Core Data to store my data. I've followed the guide in the video but unable to complete the final steps.

/// The core data stack `Data`

    var dataProvider: datasProvider = .shared

    

    func handle(intent: CreateNoteIntent, completion: @escaping (CreateNoteIntentResponse) -> Void) {

        let title = intent.title!

        let isBookmark = intent.isBookmark!

        let photo = UIImage()

        

        let newIntent = Task { await self.dataProvider.addData(time: Date(), title: title, isFavorite: isBookmark as! Bool, image: photo) }

        

        let response = CreateNoteIntentResponse(code: .success, userActivity: nil)

        response.noteIntent = newIntent //Error: Cannot assign value of type 'Task<(), Never>' to type 'Type'

        completion(response)

    }

I'm using SwiftUI and it is for iOS app. Does anyone have a solution for this or can explain to me what went wrong please. Spent few hours figuring this out. Any help will be appreciated. Thank you.

Accepted Reply

Thank you Ricob. Created a new func in CoreDataStack without async to save data from Shortcuts and all data are saved in CoreDataStack successfully.


func handle(intent: CreateNoteIntent, completion: @escaping (CreateNoteIntentResponse) -> Void) {

        let title = intent.title!
        let isBookmark = intent.isBookmark!

        /// Access core data stack `Data` to save the note contents
        var dataProvider: datasProvider = .shared
        dataProvider.addDataFromShortcut(time: Date(), title: title, isFavorite: isBookmark as! Bool)        
        
        let response = CreateNoteIntentResponse(code: .success, userActivity: nil)
        completion(response)

}

Replies

It looks like your custom response noteIntent property is being assigned an incorrect value. Task would not be a valid type for an intent parameter unless it is defined as a custom type. You are given the createdNote property which may be what you are looking for. You can also file a Technical Support Incident so that someone can take a closer look at the issue you are having.

Thank you Ricob. Created a new func in CoreDataStack without async to save data from Shortcuts and all data are saved in CoreDataStack successfully.


func handle(intent: CreateNoteIntent, completion: @escaping (CreateNoteIntentResponse) -> Void) {

        let title = intent.title!
        let isBookmark = intent.isBookmark!

        /// Access core data stack `Data` to save the note contents
        var dataProvider: datasProvider = .shared
        dataProvider.addDataFromShortcut(time: Date(), title: title, isFavorite: isBookmark as! Bool)        
        
        let response = CreateNoteIntentResponse(code: .success, userActivity: nil)
        completion(response)

}