[MacRuby-devel] Would a macruby-newbie List Be Worthwhile?

Brian Chapados chapbr at gmail.com
Wed Dec 16 14:25:43 PST 2009


On Wed, Dec 16, 2009 at 9:11 AM, steve ross <cwdinfo at gmail.com> wrote:
> 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
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>

If MacRuby is your first exposure to mac programming and all of the
associated tools and techniques, starting out by jumping into an
IB-based project that uses CoreData & Bindings is going to be really
confusing.

I'd break up the tasks into pieces small enough that you can actually
understand what's going on. You shouldn't feel like things are
happening "magically". If that is the case, then try to take a step
back and figure out how the magic trick works.

For example, if you want to tackle CoreData first, then start with
just CoreData.  Build a command-line program that creates a CoreData
stack by reading in your compiled .xcdatamodel (or create the model in
code!), add some data, and save it.  Once you've got that working,
then make it download data from your web service and save it.

Here is the approach I would take:

1. If you haven't already, read through the Core Data Programming guide:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html

At the very least, browse through this to get a feel for what's going
on.  You won't understand everything, but at least you'll have some
sense of where to look when you get stuck.

2. Read the Core Data Utility tutorial:
http://developer.apple.com/mac/library/documentation/cocoa/conceptual/CoreDataUtilityTutorial/Articles/00_introduction.html

This gives you a basic Obj-C version of a program the uses CoreData
entirely through the API.  Everything is done in code, even creating
the model, so it should get you started on something basic.

3. As it turns out, Matt Gallagher (cocoawithlove.com) recently posted
an excellent article on parsing CSV files and importing the data into
CoreData: http://cocoawithlove.com/2009/11/writing-parser-using-nsscanner-csv.html

I highly recommend you also download and read through his code to see
how it works.

4. Use the code from #2 & #3 to write a minimal implementation in
MacRuby that imports data into your model.

5. After getting #4 to work, go back and read the section on
efficiently importing data in the CoreData programming guide and
refactor your import code appropriately.

At least this way, if you get stuck on #4 & #5, you'll have a fairly
short, self-contained example program that you can put in a gist, post
a link, and ask for help on specific parts.


More information about the MacRuby-devel mailing list