Copy Realm object without adding to database - objective-c

In my app the problem is that user can edit an object, but if user pressed "Cancel" I need to discard changes, if there any method to make local copy of realm object without adding it to RealmBase, or I should do all the copies manually?

If your model doesn't have references to any other models or collection of objects, you can use this method to initialise new instance with values stored in Realm.

Related

When should ViewModel call Service methods?

For example, there is a ViewModel that has
a collection of objects requested from a Service
an Add command to create and add new objects
a Delete command to delete selected objects
The User creates, adds and deletes objects by using corresponding commands.
How should ViewModel react on User's actions?
Should ViewModel immediately make a corresponding Service method call when the User invokes a command?
OR
Should ViewModel add / remove items from local collection, mark items as added / deleted and wait to make a Service method call only when the User invokes some Save / Apply changes button?
The second variant seems more complicated.
Does it give any performance / user experience benefit or enable any features that are not possible with the first variant?
In other words, is it better to keep ViewModel state as close to the Service / Database state as possible?

One Core Data Entity Per NSPersistentDocument?

What's the best way to use Core Data if each document on disk corresponds to one Entity instance?
I have a data model file with one entity, and that entity has one attribute of name text and of type Text.
I have a Document.xib that has an NSObjectController that is set to 'Entity' mode and gets the managedObjectContext from the File's Owner. I have an NSTextField that is bound to the Object Controller for the Controller Key 'selection' and the Key Path 'text.' (This is just a test so I can figure out how Core Data works, but my eventual app will also only have one Entity instance per Document)
When I create a new document the textfield says 'No Selection' and is disabled.
I imagine that if I had a Table View or some other kind of way to select from among entity instances the selection would work but I don't nor do I want to. How can I hook up the NSObjectController to only have one Entity instance and to automatically 'select' it?
The intended behaviour is that I type something into the NSTextField, hit Save, close the document, re-open the document and the string in the textfield persists.
This is probably a really basic question but I can't find any tutorials or documents that would address this seemingly simple use case.
Ok, well I haven't figured it all out but my particular issue was being caused by the fact that nothing was being created. I switched out the NSObjectController for an NSArrayController, created an outlet for it in Document.m and added this to windowControllerDidLoadNib:
if (![self.arrayController selectedObjects]) {
[self.arrayController add:#""];
};
Now it seems to just manage the one Entity object.

Using NSArrayController without Core Data

I'm using Core Data in my OS X application where I have some TableViews bound to NSArrayControllers. The problem I'm having is when I'm trying to populate a tableview in a sheet using an array controller where I don't want the contents to persist.
Here's how the app hangs together;
Window 1 - Shows a list of users in a table view and allows adding and removing users. Contents persist via Core Data bindings.
Window 2 - Shows a list of groups in a table view. A second table view shows a list of users that belong to the selected group. Contents persist via Core Data bindings. An 'add users' button invokes a sheet for adding users to the group.
Add Users sheet - This sheet shows a table view of users that are not already members of the selected group. Pressing the close button on the sheet adds the selected users to the selected group.
Ok, so the problem I'm having is with the array controller for the Add Users sheet. When I invoke the sheet I iterate through all users and add any to the array controller if they don't already exists in the group. When I close the sheet I try to clear down the array controller using removeObject: but this causes a "can't use this method with a ModelObjectContect."
Why do I need a MOC to remove items from the array controller? It's only for display purposes so I don't need it to persist. If I set the array controllers MOC to that of my app delegate, it physically deletes the users, which I obviously don't want. I just want to remove them from the table view of the sheet.
I thought the answer might be to create another MOC to use as a scratch-pad and not tie it to a persistent store, however this just gave me a different error when using removeObject:, something along the lines of "can't remove objects that exists in another MOC."
Why am I allowed to add object to an array controller but not remove them? In cases where you don't actually want the items physically removed are you supposed to access the the underlying "content", e.g. [arraycontroller content]? I've played with this but get strange display results as it seem to be playing with the array controller's content behind it's back. If I do this, is there a way to tell the array controller "by the way, I've been tinkering with you're content and you may need to get yourself together"?
It looks to me like you shouldn't be using array controllers without Core Data, but there is numerous comments in the documentation that suggests that it works with and without core data.
Yes, you can use an array controller without a Core Data Managed Object Context. But as you're storing NSManagedObject instances inside it, I think it tries to mark them for deletion when you removed them.
If you work with managed objects and don't want the contents of the array controller to be deleted on removal, you have to bind the array controller's content to another object's property with Cocoa Bindings.
But there is a simpler solution. I suggest you to set the managed object context of the array controller to your main MOC and use a predicate to filter its content.
[arrayController setPredicate:[NSPredicate predicateWithFormat:#"NONE groups == %#", group]];
Thus, there is not need to add or remove users from the array controller as all users that are already in the group will be hidden.
You can use them with and without core data, but an array controller either uses core data (entity backed), or it doesn't. I don't think you can use one with managed objects and not have a context.
I'm not clear why you are creating objects instead of just using a fetch request?
You don't say how you are adding the "missing" users but If this is just a basic list, you could consider creating an array of proxy objects (so you aren't touching the MOC) which you can junk when the sheet is done. You could use a non-core data array controller for this, or just (gasp!) not use bindings at all and do it the old fashioned way.
Why not use [arrayController setContent:nil]

Sheet and WindowController Delegates - Is this a good approach?

I'm using a sheet to ask the user some information for an operation that the application performs. The sheet has some 10 to 15 options (split into 4 tabs, so its a relatively clean UI) that the rest of the program needs to know before proceeding.
Right now, I have a separate window controller class for the sheet called SheetController. SheetController has a delegate property and the main controller, AppController, is set as the delegate.
When the user clicks OK in the sheet the delegate is notified and a method called didClickDone:(id)sender withParameters:(id)parameters is executed. parameter is a object that contains various properties from the sheet. My question is, is this a good approach to handling Sheets and returning data from them?
Secondly, one thing that bothers me is that the parameter is just a dead object - it only has accessor methods. It doesn't do any manipulation on them as its entire purpose is to 'carry' the data to the main controller, which in turn passes the information to the Model of the program. And, unless I'm missing something, shouldn't I just declare parameter to be a normal C struct? Or is there an advantage of using an object for this sort of purpose?
EDIT: Would a passing an NSDictionary be a good compromise? I could save the returned information with their keys in the dictionary and simply pass it.
Maybe I would not notify directly the controller from the push button event handler but
call another routine that encapsualates the logic on how the data have to be commited to the model.
Don't worry about the empty object. Is a common pattern to use it and is called DataTransferObject

Updating NSTableView when enitiy is added to core data

I have a Cocoa App that I have manually added core data to. I setup the table in Interface Builder to list the entities from the data (with NSArrayController), and this is working just fine. The problem is when I insert a new entity (via code) the table does not update until I restart the app.
What do I have to do after inserting an entity and saving the context to get the table to automatically pick up the changes?
I'll assume you mean you want to update your array controller's contents, allowing the table to update as a result.
Short answer: Send your array controller a -fetch: message.
Longer answer: Only entity instances added through an array controller automatically show up in its contents array when it gets its contents via a straight fetch request (ie, when its contents array isn't bound to anything, but rather you set an entity name and a MOC, possibly a predicate, and nothing else).