UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Passing Core Data object to userInfo dictionary in notification

Forums > 100 Days of SwiftUI

My app triggers notifications generated when saving Core Data Objects. I'm trying to get to the detail view of the object that scheduled the notification after tapping on it, through AppDelegate mehods. I have been trying with object UUID():

content.userInfo = ["ensayoId" : nuevoEnsayo.id!.uuidString]

But, is there any way to "link" the object itself? I mean:

content.userInfo = ["rehersalId" : rehersal]

where "Rehersal" is an instance of Core Data Entity "Rehersal"?

1      

I have make any improvements with this:

I'm getting my Core Data object id with

content.userInfo = ["objectId" : myObject.id!.uuidString]

Capturing back its value, after tapping on the notificacion, on AppDelegate with

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
//
if let objectId = userInfo["objectId"] as? String {
//(1)
//

And implementing a method on the view I want to be responsible of presenting data, using a FetchRequest with a predicate to match that id, and extracting it from de array

struct DetailView: View {
@Environment(\.managedObjectContext) var moc
@ObservedObject var object: Entity
//

public mutating func editFromId(id: String) {

//(2)
@FetchRequest(sortDescriptors: []) var objects: FetchedResults<Entity>
objects.nsPredicate = NSPredicate(format: "%K == %@", (\Entity.id) as! CVarArg, id)
object = objects.first ?? Entity()
}
//

But now, how could I "connect" object obtained on AppDelegate (1) for using it on the view (2)? How can I invoke editFromId funcion from AppDelegate?

I'm trying this, but unsucesfully, on AppDelegate

let detailView: DetailView?
detailView?.editFromId(id: objectId)

as it says "Constant 'detailView' passed by reference before being initialized".

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.