0

I created a simple SwiftUI app with Core Data and I want to be able to add data via the shortcuts app, I created a shortcut that takes some text as input and returns it in uppercase and when I run the shortcut in the shortcuts app, it works, however when I added an "add" function (to save data in the Core Data database) to the intent handle function, and I run it again nothing is saved in the app, here is the code:

class MakeUppercaseIntentHandler: NSObject, MakeUppercaseIntentHandling {
    let persistenceController = PersistenceController()
    func handle(intent: MakeUppercaseIntent, completion: @escaping (MakeUppercaseIntentResponse) -> Void) {
        if let inputText = intent.text {
            let uppercaseText = inputText.uppercased()
                    
            completion(MakeUppercaseIntentResponse.success(result: add(text: uppercaseText)))
            
        } else {
            completion(MakeUppercaseIntentResponse.failure(error: "The text entred is invalid"))
        }
    }
    
    func resolveText(for intent: MakeUppercaseIntent, with completion: @escaping (MakeUppercaseTextResolutionResult) -> Void) {
        if let text = intent.text, !text.isEmpty {
            completion(MakeUppercaseTextResolutionResult.success(with: text))
        } else {
            completion(MakeUppercaseTextResolutionResult.unsupported(forReason: .noText))
        }
    }
    
    func add(text: String) -> String{
        let newItem = Item(context: persistenceController.container.viewContext)
        newItem.text = text
        
        do {
            try persistenceController.container.viewContext.save()
        } catch {
            let nsError = error as NSError
            fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
        }
        
        return text
    }
}


import CoreData

struct PersistenceController {
    static let shared = PersistenceController()

    let container: NSPersistentContainer

    init() {
        container = NSPersistentContainer(name: "SiriShort")

        guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.SiriShortcut2")?.appendingPathComponent("SiriShort.sqlite") else {
            fatalError("Shared file container could not be created.")
        }
        
        let storeDescription = NSPersistentStoreDescription(url: fileContainer)
        storeDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
        storeDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
        
        container.persistentStoreDescriptions = [storeDescription]
        
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        
        container.viewContext.automaticallyMergesChangesFromParent = true
        container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
    }
}

Thank you

Jad
  • 66
  • 6
  • You haven’t given us much to go on here, is add(…) even called or what happens? Have you debugged this or used print statements to follow what code path is taken and what data is being passed? – Joakim Danielson Dec 08 '21 at 09:00
  • I tried to use print statements however it's not printing anything because when I am running the shortcut I am outside the app – Jad Dec 08 '21 at 09:15
  • The add(…) function is executed (because the uppercased text is being returned) but it is not saving anything to the database – Jad Dec 08 '21 at 09:20
  • I just solved it, I forgot to add App Groups, however the app isn't refreshing, to see the new data I have to close it and reopen it, any clue on how to fix this issue, I will post my CoreData Stack – Jad Dec 08 '21 at 09:33
  • Provide your solution or ask a new question if a new problem has arisen. – Mac Wayne Dec 15 '21 at 04:01

0 Answers0