如何从dataController中显示.Alert

发布于 2025-01-23 17:59:11 字数 1836 浏览 2 评论 0原文

我正在尝试在“ datacontroller”文件中设置警报,而没有任何成功!

在SwiftUi视图中,我可以轻松地这样做,

// Before the body
@State private var showAlert = false

// Inside the body
.onTapGesture { showAlert.toggle() }
.alert("There is an error", isPresented: $showAlert) { }

但是在我的datacontroller中,我实际上不知道如何执行它:

// DataController.swift
struct DataController {

    static let shared = DataController()

    let container: NSPersistentContainer

    init(inMemory: Bool = false) {

        container = NSPersistentContainer(name: "ItemModel")

        if inMemory {
            container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
        }

        container.loadPersistentStores(completionHandler: { (storeDescription, error) in

            if let error = error {
                // SOMETHING HERE?
                print("Core Data failed to load: \(error.localizedDescription)!")
            }

            // print("Core Data succeed to load!")

        })

        container.viewContext.automaticallyMergesChangesFromParent = true

    }

    func save(context: NSManagedObjectContext) {

        // try? context.save()

        do {
            try context.save()
            // print("Core Data succeed to save!")
        } catch {
            // SOMETHING HERE?
            print("Core Data failed to save!")
        }

    }

    // Some other functions below...

}

您有任何想法吗? :d

[edit]

这是我的函数创建,例如:

func createItem( /* Some stuff here */ ) {

    let item = Item(context: context)

    /* Some stuff here */

    save(context: context)

}

这就是我在swiftui视图中称其为:

// Inside a button
DataController.shared.createItem( /* Some stuff here */ )
presentationMode.wrappedValue.dismiss()

I'm trying to set up an alert inside my `DataController' file, without any success!

In a SwiftUI View, I could do it easily, like that :

// Before the body
@State private var showAlert = false

// Inside the body
.onTapGesture { showAlert.toggle() }
.alert("There is an error", isPresented: $showAlert) { }

But in my DataController, I literally don't know how to perform it:

// DataController.swift
struct DataController {

    static let shared = DataController()

    let container: NSPersistentContainer

    init(inMemory: Bool = false) {

        container = NSPersistentContainer(name: "ItemModel")

        if inMemory {
            container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
        }

        container.loadPersistentStores(completionHandler: { (storeDescription, error) in

            if let error = error {
                // SOMETHING HERE?
                print("Core Data failed to load: \(error.localizedDescription)!")
            }

            // print("Core Data succeed to load!")

        })

        container.viewContext.automaticallyMergesChangesFromParent = true

    }

    func save(context: NSManagedObjectContext) {

        // try? context.save()

        do {
            try context.save()
            // print("Core Data succeed to save!")
        } catch {
            // SOMETHING HERE?
            print("Core Data failed to save!")
        }

    }

    // Some other functions below...

}

Do you have any idea? :D

[EDIT]

Here's my function create, for example:

func createItem( /* Some stuff here */ ) {

    let item = Item(context: context)

    /* Some stuff here */

    save(context: context)

}

And here's how I call it in a SwiftUI View:

// Inside a button
DataController.shared.createItem( /* Some stuff here */ )
presentationMode.wrappedValue.dismiss()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文