[MacRuby-devel] Would a macruby-newbie List Be Worthwhile?
steve ross
cwdinfo at gmail.com
Wed Dec 16 09:11:52 PST 2009
On Dec 16, 2009, at 1:07 AM, John Shea wrote:
>
> The second part, filling with data (presumably you will only need to do this once, because then the data can be saved with the app).
> There are many ways to add data.
>
> The easiest I reckon, is to add a method to the AppDelegate - called applicationDidFinishLaunching(notification) (delegated from the application singleton) and in that method you can do something like:
> new_student = NSEntityDescription.insertNewObjectForEntityForName("Student", inManagedObjectContext:self.managedObjectContext).
> Then set the attributes for that obj, eg student.name = "Bill".
Let me be more specific about *why* this is important to me. My app goes out to a Web Service for data, so I have to fill some of it in programmatically, then it refreshes only occasionally. I decided this would be best in the controller, so it could be triggered by the user. To emulate that, I wanted to prepopulate the managedObject collection, and that I did in awakeFromNib. Warning: Make sure to call super! So what it boiled down to what this method in the controller:
def new_image(image = {})
object = NSEntityDescription.insertNewObjectForEntityForName("Image",
inManagedObjectContext:managedObjectContext)
object.setValue image[:fileid], forKey: 'fileid'
object.setValue image[:title], forKey: 'title'
puts "#{object} #{object.fileid} : #{object.title}"
end
I used the setters explicitly when I was confused about why the bound tableview was blank. I'm beginning to get this a bit more under control, but the IB/CoreData/Cocoa Bindings is even magical to a Ruby person :)
See? I told you it was a dumb question.
Thanks,
Steve
More information about the MacRuby-devel
mailing list