如何从dataController中显示.Alert
我正在尝试在“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论