Re: [MacRuby-devel] Create new Core Data Instances
Hi Timo,
a) Is it really THAT complicated to create an instance of MyEntity and set the attribute? I'm coming from the Rails/ActiveRecord world where this could easily be done with a single line of code (including saving the instantiated object): MyEntity.new(:attribute1 => "Test").save )
b) How can I save this fresh instance and update my NSTableView?
After you have modeled your entity in the Xcode core data modeling view, you can use instance = NSEntityDescription.insertNewObjectForEntityForName( "MyEntity", inManagedObjectContext:managedObjectContext ) to instantiate an instance. You can save it calling error = Pointer.new_with_type("@") managedObjectContext.save(error) To save attributes, simply assign to them: instance.attribute1 = 'Test' This assumes you've added that attribute to your model. The managedObjectContext can be created or, if you're using a core data project like the document one, you'll have a managedObjectContext accessible from your NSDocument subclass: self. managedObjectContext If you have bound your NSTableView to the model object, changes will be picked up automatically. You'll only need to call save to actually persist to disk. I hope this helps somewhat, there's much more detail to it and the following links explain more of those: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreD... and some MacRuby specific ones that I've found useful in the past http://www.springenwerk.com/2008/10/macruby-and-core-data-tutorial.html http://cyberfox.com/blog/120-how-i-learned-to-stop-worrying-about-core-data-... http://reborg.tumblr.com/post/263347770/macruby-coredata-tutorial Cheers, Sven
Hi, Am 14.11.2011 um 09:47 schrieb Sven A. Schmidt:
a) Is it really THAT complicated to create an instance of MyEntity and set the attribute? I'm coming from the Rails/ActiveRecord world where this could easily be done with a single line of code (including saving the instantiated object): MyEntity.new(:attribute1 => "Test").save )
b) How can I save this fresh instance and update my NSTableView?
After you have modeled your entity in the Xcode core data modeling view, you can use
instance = NSEntityDescription.insertNewObjectForEntityForName( "MyEntity", inManagedObjectContext:managedObjectContext )
to instantiate an instance. You can save it calling
error = Pointer.new_with_type("@") managedObjectContext.save(error)
To save attributes, simply assign to them:
instance.attribute1 = 'Test'
This assumes you've added that attribute to your model. The managedObjectContext can be created or, if you're using a core data project like the document one, you'll have a managedObjectContext accessible from your NSDocument subclass: self. managedObjectContext
I've already added attributes to my model. I'm not sure about this manageObjectContext thing. I've created the Entity with it's attributes in IB. I added an NSTableView to my window and connected it to my model. Objects are displayed in the TableView and I also can add record directly in the table (connected the add: action to a button). So my model seems to be fine. Next step would be to create an object more manually (i.e. for some kind of Import functions...). I'm not sure where I get this manageObjectContext from in my current situation or how to create one. It also seems like I should dig a little more into Core Date and check how it works... currently it seems a lot more complicated than ActiveRecord to me...
If you have bound your NSTableView to the model object, changes will be picked up automatically. You'll only need to call save to actually persist to disk.
I hoped that would be the case. Good to know it is.
I hope this helps somewhat, there's much more detail to it and the following links explain more of those:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreD...
I'll take a look.
and some MacRuby specific ones that I've found useful in the past
http://www.springenwerk.com/2008/10/macruby-and-core-data-tutorial.html http://cyberfox.com/blog/120-how-i-learned-to-stop-worrying-about-core-data-... http://reborg.tumblr.com/post/263347770/macruby-coredata-tutorial
Thanks for the links. I've already found and read them before. But they didn't answer my questions (or I were too blind to see it). I've got feeling that I've read every single page about MacRuby that Google will find ;-) Regards, Timo -- twitter.com/orangeorb
participants (2)
-
Sven A. Schmidt
-
Timo Springmann