开发者

Updating a TableView with a WebService and Saving to CoreData

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 stor开发者_StackOverflow社区e, 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.


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.


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: 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜