views:

69

answers:

2

I am working on a project where I have a table view that is currently updated via a web request that returns XML. I implemented

-(int)numberOfRowsInTableView:(NSTableView*)tv

and

 -(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn*)tableColumn row:(int)row

in my XML parsing class, and have the table updated with the data that is pulled down from the server.

I want to save the data that is pulled down using Core Data, so that the table can be saved/loaded. Then later on application start when the web request is made, it will only add data that is not already present. (The XML is sorted by release date, so later I will check to see which release dates are not loaded up from the Core Data store, and only load newer entries.)

How would I go about implementing this? I am a very new Cocoa developer, but have gone through the entire Hillegass book. Thanks so much.

+2  A: 

There are two big pieces that you're talking about here: parsing XML, and persistence with Core Data.

(1) I have had some success using this wrapper around NSXMLParser to read XML files. I've heard of but have not used more recent libraries, but this may provide a starting point for you. The linked article is quite thorough about usage.

(2) The first thing you will want to do with Core Data is create a new data model. From there, you can create an model class (with @dynamic properties) to easily interact with your database through Core Data (using things like NSManagedObjectContext and its ilk). You can get a gentle introduction here, or jump in a little further along here.

Implementing Core Data is non-trivial, especially for newer developers. I'd encourage you to seek out tutorials on particular topics as they arise.

warrenm
Thanks for the advice. Right now I have things somewhat working, but I feel like I am using Core Data in an extremely archaic way (creating managed objects, setting their values, using predicate searches to get objects I need...). I think the use of @dynamic properties can simplify what I'm doing now.
jcady
dynamic properties are useful shorthand, similar in one sense to synthesized properties. What you describe doesn't necessarily sound archaic. In any event, you'd have to write some glue code for transforming objects from the XML description to managed entities in Core Data, which sounds like your approach.
warrenm
Mac programmers such as the questioner do not need that wrapper, as Cocoa, unlike Cocoa Touch, already has an NSXMLNode class. http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSXMLNode_Class/
Peter Hosey
+1  A: 

I'm in the same boat as you right now - I'm just learning Cocoa, I've read the Hillegass book, and I'm building a Core Data app that pulls from a Web Service. I've figured out how to get my app populating a table view from a web service successfully, but I have no idea if the way I'm doing is the right way or best practices. Here's what I did:

I created a Core Data model with the same attributes as the entity coming from the Web Service. In Interface Builder, I created a table view and set up the bindings between the table columns and the Core Data attributes. When the app launches, I pull down the data from the Web Service, loop through the items, and create new entities. The table view then automatically recognizes the new data and populates itself.

The Core Data book from Pragmatic Programmers, as well as this question both helped me: http://stackoverflow.com/questions/2563984/json-to-persistent-data-store-coredata-etc

I haven't tackled the syncing issue (adding new items, updating existing) but I have this doc bookmarked for when I do: http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdImporting.html#//apple_ref/doc/uid/TP40003174

Nick Wientge
Thanks for the tip. Based on the answers here and some tutorials I read, I am doing a similar thing. I'm looping over the XML nodes and creating appropriate managed objects.
jcady