开发者

Best approach for implementing Insert/Edit of Core Data Entities

开发者 https://www.devze.com 2023-04-03 21:58 出处:网络
Let’s suppose there is an entity called PER开发者_高级运维SON in a CoreData model and a NSPanel (displayed as a Sheet) is used to ask for the information needed to create an instance of such entity.

Let’s suppose there is an entity called PER开发者_高级运维SON in a CoreData model and a NSPanel (displayed as a Sheet) is used to ask for the information needed to create an instance of such entity. The NSPanel also has a Save and Cancel buttons.

What would be the best way to implement this?

My current approach is to create the NSPanel and add outlets to NSTextField’s corresponding to the FirstName and LastName attributes of the PERSON Entity.

For the Save button I’ve created a Save Action that “reads” the data entered by the user through the Outlets and manually create a PERSON Entity with the provided information.

For the Cancel button, I’ve created a Cancel Action that just closes the NSPanel.

Even though the whole thing works as intended, I find this approach messy. Things get even worse with the need to implement the Edit functionality since a new method is needed to read the data from the selected Entity and pass it to the NSTextField’s, and add the required logic to the Save Action to account for the update operation.

I wonder if a full CoreData/Binding approach is possible in this scenario?

The main issue with the Full CoreData/Binding approach, at least to my short experience on these frameworks, is the fact that once the user start entering the info on a new instance, is already to late Cancel operation. Right?


The setup you describe is by no means messy. Rather, it provides ample opportunity to enhance the UI experience.

Your last point, or "main issue", is not a problem at all. You can put up the NSPanel and have the user input new values, and simply dismiss them if Cancel is pressed. Only if the Save button is pressed do you insert a new object into the core data managedObjectContext and save.

Similarly, when editing you simply retrieve the data before and populate the fields. You keep a reference to the record, if Save is pressed you modify the attributes and save the managedObjectContext. If the operation is canceled you simply discard the reference.

0

精彩评论

暂无评论...
验证码 换一张
取 消