|
I'm trying to edit some Core Data object values (ratings and a text), obtaining them from a local notification. To do so, I have added some actionIdentifiers to my notification request, which are handled this way on AppDelegate:
On my MainView (the main view of my tabbed app), I've written this method:
I do can access this three pieces of data on my view, but the question is that I can't find the way to make the fetch request, with its predicate, select that exact object (Rehearsal), with that id, to set its new values. The above code, for example, says "Ambiguous reference to member 'id'", although I've tried with many different ways of fecth request sintax and so, without success, many of them finishing with the text I write on the title of this post. Any ideas? Thanks in advance. |
|
The error message or warning in the title should come from I don't think you have to do your work in |
|
Thank you, Hatsushira. This is one of the options I'm working on. In fact, the code is not on userNotificationCenter, but on AppDelegate. But I'm not able to understand completely how. As far as I know, notifications are handled exclusively by AppDelegate, aren't they? Or you suggest adding this method to a new class, and adding my "actionIdentifier" control there?
But, in that case, I still can't see the exact way to instantiate this method "inside" my View. Could you please give me any guidance code? Thanks again. |
SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
In adittion, this must be possible directly in AppDelegate:
But just one problem I can't solve for weeks: I need a moc, a ManagedObjectContext, which is added to the environment the way Paul explains, but absolutely inaccesible from AppDelegate class:
How can I access the singleton of that moc, the unique instance used on all the app, and available on environment? I can't find the answer to this question after reading dozens of articles and web pages. Nobody knows? May is it impossible? |
|
Yes, you should create a new class implementing
You could handle it all on your AppDelegate but I would create an instance of your new class in your View. That's what the delegate is used for. I'm not an expert at it but for me it worked with my own NotificationHandler.
You need to pass it via initializer. You can't access EnvironmentObjects outside of a View. These only work in a View. You can't access EnvironmentObjects in the initializer of the View, either. This is working as intended. |
|
Thanks! Trying your indications. Now I have a new class to handle notifications, away from AppDelegate:
I have commented all the same code on my AppDelegate file, in order not to confuse the code. But now, after notification taps, nothing occurs. It seems that this new class is not handling them correctly, as I control with breakpoints the running on XCode and it doesn't stop in this method. What am I missing? |
|
Maybe is this the problem?: Only one class can "work as" appDelegate. But, as I have another methods on AppDelegate to handle quick actions from home icon (shortcuts), how can I handle both in differente classes? 🧐 |
|
I set also
In the AppDelegate. My AppDelegate is
My Notification Manager is only
|
|
I finally have found a much better solution. Not code, but logic:
So now I can handle perfectly all stuff (notifications reponses, home quick access actions...) from here. The curious thing in conclusion: I have read up to hundred articles trying to find a solution for this issue, but nobody has seen the solution "out of the box", or "upside down" ;-) Anyway: thank you very much @Hatsushira for your ideas, which has been crucial to get to the easiest solution. |
|
I'm not sure what will happen if you open the same container twice. I would guess you need the CoreDataStack in your views as well. How do you pass it to your views? |
|
I keep both: passing Core Data context to app environment (@Environment(.managedObjectContext) var moc in my views):
And using it directly on AppDelegate class itself, for notifications handling:
ALTHOUGH, in fact, I'm seeing some strange behaviour with Core Data saved through AppDelegate class 🧐. If I save data using usual environment way in my views, and later update the same data using AppDelegate method, some cases work, some not, missing data. Do you suggest that this way would drive to two (or more) different instances of viewContext running on the app the same time? I suposse that the instance I use on AppDelegate (marked with asterisk on the above description) is the same as environment one. But, you are right!, it is not initializated there 😅. Any further suggestions? Thanks again! I'm learning a lot with your orientations. Let's try to make it work at last 😊. |
|
The strange behaviour in your data could be because you open the container twice and have two instances running. I looked up my old code and I missed a part. As I told you I have a class NotificatonManager.
In the init method of this class I call
You should be able to add an additional init with the viewContext. Additionally (that I overlooked and forgot) I have an extension on my NotificationManager
These functions should be responsible when for the events that trigger when a notification is received. So you should be able to do this in your view with a separate class. |
|
Thanks again, @Hatsushira. I'm giving a try again to your proposal. I have now this NotificationManager class, with your indications:
Is it correct? How can then I accomplish what you suggest, then? is it necessary? I know about the "singleton" technique (an static var for instanciate viewContext) from UIKit. I've tried, but can't make it match with SwiftUI.
Please, sorry me for my so precise questions 😅, but I'm starting getting crazy with this, as I feel I am walking "on circles". Thank you very much anyway for all your time an orientatios 🙏. |
|
Well, you said, that you have to check in your data how to handle the information according to the actionIdentifier. So somehow there must be a reference to your DataController to get the data. This is where SwiftUI is not really optimised yet. I guess the NotificationManager is in a View. As I said above, you can't access EnvironmentObjects in the init of a View. They don't exist in the init. There are two (or more ways) to achieve what you need.
So in your View you declare variables:
then the init of this View should be like
Please excuse syntax errors. I don't have an editor available.
I would go with the first one. Perhaps, there are more ways but unfortunately, I'm out of ideas :) |
|
Thanks for your patience, @Hatsushira. "I guess the NotificationManager is in a View". No: it is and standalone class itself. But, I've dealing with the second option you suggest, after triple-cheaking this post: https://en.proft.me/2021/05/26/using-core-data-swiftui/, and I have added a .shared instance of DataController:
... as my DataController class has now a singleton: an static let.
This way, I can use both @environment tecnique for views:
... and THE SAME singleton on NotificationManager class. I'm now testing the app and everything seems to work perfectly: no strange duplicated contexts, Core Data persistence, notifications handling... If you thing this way will not throw errors on the future, we could say together "voilá, it's done" Thanks again 😊 |
|
SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.