#StateObject for a NSManagedObject without context not publishing changes - core-data

Like in a lot of apps, I have a list of items (populated by a Core Data fetch request), a sheet to create new items, and a sheet to edit an item when tapping on a row in my list. I'm trying to unify both forms to create and edit an update, and put the cancel / save logic in a superview of the form.
So I've something like this:
ListView: a list with row populated by a Core Data fetch request
AddView: a NavigationView with the FormView embed + cancel and save button
EditView: a NavigationView with the FormView embed + cancel and save button
FormView: a TextField to update the name of the item
In the init() for the AddView, I create a new NSManagedObject without any context (I do that because I don't want my ListView to be updated when I create a new item in the AddView, but only when I save this item -> alternative could be to use a child context, or filter the fetch request results based on the isInserted or objectID.isTemporaryID of the return objects). AddView contains a NavigationView with the FormView embed, a cancel button, and a save button. This save button is disabled based on a computed property on the managed object (name for the object can't be nil).
In the EditView, I pass the item that was tapped from the ListView. This item is an existing NSManagedObject attached to the main viewContext of the app (coming from the fetch request of the ListView). EditView contains a NavigationView with the FormView embed, a cancel button and a save button (exactly like the AddView). This save button is also disabled based on the same computed property.
My issue is that when I update the name of the item from the TextField in my FormView, the condition to enable / disable the save button is not working for the AddView (this AddView is actually not refreshed when I change the item name from the FormView) but working for the EditView (this EditView is refreshed when I change the item name from the FormView). If I attach a context to the new NSManagedObject in the init() of the AddView, the condition is working like in the EditView.
So it appears that a NSManagedObject without any context is not observed by SwiftUI? Am I missing anything or is that a bug?

I wouldn't be surprised (but haven't verified) if the change-notifying ability of a managed object depends on the presence of the context. I can't think of a situation where you'd want to create a managed object without a context.
You should use a child context. The context does a lot of work for you in Core Data (managing relationships, probably change notifying, validation etc), and offers a simple way to cancel / save changes - just save the child context and the data flows back up into the main context, or discard the context to abandon.

A workaround to get the change notifications is to add this override to the NSManagedObject subclass:
override public func willChangeValue(forKey key: String) {
super.willChangeValue(forKey: key)
self.objectWillChange.send()
}
NSManagedObject could be subclassed to add this override (more info here and here)
We can be more specific on the update value if we don't want to trigger the change for every key. This will also work for relationships (not the case for the above solution).
func setName(_ name: String) {
objectWillChange.send()
self.name = name
}
In this case, my AddView updates even if the observed object does not have a context (change notifications are probably trigger only when a context exists for the object). The save button is disabled / enabled based on the following computed property in my NSManagedObject subclass.
var canBeSaved: Bool {
if self.name.isEmpty {
return false
} else {
return true
}
}

Related

How to automatically create Note record in Acumatica?

I've noticed that whenever an AR Invoice gets saved, a record gets created in the Note table with the new invoice's note ID. Can you tell me how that is being accomplished? I'd like to get one of my screens to do the same thing. I guess there must be some kind of attribute on the either the DAC or the graph but I can't find it. I have the PXNote attribute on the NoteID column in my DAC but it does not cause a Note record to be automatically created.
Thanks for your help.
To have Note record automatically created when a new parent record gets saved, one should invoke the static PXNoteAttribute.GetNoteID<Field>(PXCache cache, object data) method when the parent record is inserted in the cache.
For example, to have Note record automatically created when a new Stock Item gets saved, you should subscribe to RowInserted handler for the InventoryItem DAC and call PXNoteAttribute.GetNoteID<Field>(...):
public class InventoryItemMaintExt : PXGraphExtension<InventoryItemMaint>
{
public void InventoryItem_RowInserted(PXCache sender, PXRowInsertedEventArgs e)
{
var noteCache = Base.Caches[typeof(Note)];
var oldDirty = noteCache.IsDirty;
PXNoteAttribute.GetNoteID<InventoryItem.noteID>(sender, e.Row);
noteCache.IsDirty = oldDirty;
}
}
The code snippet above can be incorporated into almost any custom BLC with a couple simple changes to replace InventoryItem with a custom DAC.

How to add button + to subgrid of hierarchical relation?

I have a hierarchical relation inside an entity X, I Have parent lookup which allow to give parent to a record of this entity, and I have created a Subgrid attached to this lookup within the same form of the entity:
The problem is that the display of the button + is unstable in this subgrid, sometimes it appears sometimes no. I dont know if this problem is related to some setting or it is a bug of dynamics crm online last version?
For information, I don't have this problem with other sub-grids.
Thanks in advance,
if you want to add a custom button you may do this as follows
function CreateButton() {
var connectionSubGridPlusBtn = document.getElementById("Connections_addImageButton").parentNode.parentNode;
//Connections_addImageButton is the id of + button
if (connectionSubGridPlusBtn != null) {
//New Button
var div = document.createElement("div");
div.className = "ms-crm-contextButton";
div.innerHTML = "<button id='newButton' type='button' style='width:80px;cursor: pointer;padding:0px' >New Button</button>";
connectionSubGridPlusBtn.appendChild(addVendorDiv);
//Event and url for new
document.getElementById("newButton").onclick = function () {
//Write codefor the button click event
}
}
}
call this function on load of the form
The entity has to be created before you're able to add related entities. You can add disable all required fields, and perform a save in the onload, and you should always see the plus sign.
A slightly better solution is to override the create button for the entity, and rather than directing to the create form, perform a rest entity creation, then direct to that form. Then you don't have to perform a save in the on load.

Refresh Stock Items Screen

I have a problem in Acumatica. I have created a trigger on InventoryItem table to insert the inserted records to my customized table.
The problem is, whenever I try to save new stock items in Acumatica, it does not reflect the correct last-saved data of the stock item. The details in the general settings tab are incorrect. I need to close the screen and reopen to be able to see the correct data.
Can someone please help me with regards to how can I get a refreshed stock item screen immediately after saving. Or is there a bug in Acumatica whenever there is a customized trigger?
I haven't checked the issue you mentioned about not refreshing the information, but if you need to forcefully refresh the screen after saving the record
Override persist
call the base action
create a new instance of graph
search for the record to set as current of the header cache
throw new redirect required exception
so the code might look as below [Might require Modification]
[PXOverride]
public void Persist(Action persit)
{
persit();// this will call base Persist();
InventoryItemMaint grp = PXGraph.CreateInstance<InventoryItemMaint>();
InventoryItem inv = PXSelect<InventoryItem, Where<InventoryItem.inventoryCD, Equal<Required<InventoryItem.inventoryCD>>>>.Select(grp, this.Base.Item.Current.InventoryCD.Trim());
if (inv != null && inv.InventoryID.HasValue)
{
grp.Item.Current = grp.Item.Search<InventoryItem.inventoryID>(inv.InventoryID);
throw new PXRedirectRequiredException(grp, "Reloading Item");
}
}
If you dont want the whole screen to be refreshed, instead of throwing the exception you can just refresh the required view method suggested by other user(Yura Zaletskyy) on this post.
Let's say your grid is binded to view PayRollsDetails. Then you can use following code to refresh your grid:
PayRollsDetails.View.Cache.Clear();
PayRollsDetails.View.Cache.ClearQueryCache();
You can try the select method.
For example: this is the item setting data view in Acumatica source code (you can use the explore source code page)
[PXViewName(Messages.InventoryItem)]
public PXSelect<InventoryItem, Where<InventoryItem.inventoryID, Equal<Current<InventoryItem.inventoryID>>>> ItemSettings;
Through customization (I assume you use AEF), add select method for this data view
[PXViewName(Messages.InventoryItem)]
public PXSelect<InventoryItem, Where<InventoryItem.inventoryID, Equal<Current<InventoryItem.inventoryID>>>> ItemSettings;
protected virtual IEnumerable itemSettings()
{
return new PXSelect<InventoryItem, Where<InventoryItem.inventoryID, Equal<Current<InventoryItem.inventoryID>>>>(Base).Select();
}
Sometimes I use sql store procedure to insert data to my table, the select method is helpful for reloading screen with inserted data.

When using MonoMac How instantiate NSDocument

Using MonoMac, I have a NSDocument-based application, but I'm needing to create a new NSDocument object when a button is clicked.
For example. I have in another Window I have a NSWindowController and I can do
Controller c = new Controller ();
c.Window.MakeKeyAndOrderFront (this);
thus causing the Window to be loaded that is tied to the controller.
With the NSDocument I guess the controller is built in?
So I'm expecting something like
MyNSDocument doc = new MyNSDocument ("Some Value ");
doc.Window.MakeKeyAndOrderFront (this);
Of course this doesn't work.
Additional info, for example when in the Application if you hit Command + N, then a new Document Window is loaded. This is cool and I basically need the same thing to happen, but when a button is clicked.
Using "File" / "New" or Control + N invokes newDocument: on the application's shared document controller, which is the menu.xib's First Responder.
To do the same programmatically, use NSDocumentController.SharedDocumentController to get the application's shared document controller, then invoke NewDocument () on it (you can pass null as sender):
var controller = (NSDocumentController)NSDocumentController.SharedDocumentController;
controller.NewDocument (null);

Fill CoreData ManagedObject property based on another object property

I have app that stores tree structure in CoreData.
There is an ManagedObject, "Item", and it has attributes:
itemId (string)
List item
title (string)
parentId (string)
parent (relationship to Item)
parentTitle (string)
parentId points to another Item object.
How do I make property parentTitle to be filled automatically with title of parent Item ?
While Martin's suggestion is a good solution for derived values, my question on yours is, why would you want this? You are not manipulating the value from the parent at all, ever. Since you are just accessing it, access the parent directly via KVC such as:
Item *item = ...;
NSString *title = [item valueForKeyPath:#"parent.title"];
//Do something with title
The only time you would want to use the keyPathsForValues... functionality is if you are changing something based on that value. If you are just accessing it, use KVC directly.
This is a possibility to achieve the desired functionality:
// implement in Item.m
// manages KVO notifications
+ (NSSet *)keyPathsForValuesAffectingParentTitle
{
return [NSSet setWithObjects:#"parent.title", nil];
}
// getter for parentTitle
- (NSString*) parentTitle
{
return [self valueForKeyPath:#"parent.title"];
}
additionally declare the property for parentTitle as readonly in Item.h
There is no need to declare a Core Data attribute "parentTitle".
The only problem I see with this solution is the following:
Item A is parent of item B
A gets turned into fault
B is still active and some View is bound to B.parentTitle
The view gets a notification because of the dependency declared with keyPathsForValuesAffecting, still object A is already faulted (and on shutdown unable to be unfaulted again) does Core Data manage such faulting&observation problems automatically?

Resources