Neal L
Neal L

Reputation: 4339

Three20 + Core Data Simple Example

I've combed through SO, the Google group, and the blog-o-sphere trying to find an example of how to get the Three20 library to work with Core Data, and have not found much to speak of.

Does anyone here know where I could find a simple tutorial (or be willing to post one) on how to work with Core Data entities and Three20? Maybe something like:

I have an Core Data entity called Book which has the String attributes title and description. How would I create a simple app that would open with a table view showing a listing of all books, and when a row is touched push a view onto the nav controller that displays the selected book object's attributes? (just an idea -- anything that shows how to work with Core Data / Three20 would be much appreciated)

Thanks!

Upvotes: 2

Views: 1188

Answers (3)

lyonanderson
lyonanderson

Reputation: 2035

There is now a branch of three20 - CoreDataSupport - that supports using a NSFetchedResultsController. There you will find a NSFetchedResultsDataSource.

Upvotes: 0

Nick Toumpelis
Nick Toumpelis

Reputation: 2714

As far as I know (Three20 documentation is sparse), there is no automatic way for Three20 to work with Core Data.

What I typically do is:

  • get the set of entities from Core Data
  • load the relevant data (from the entities) onto a TTTableViewDataSource (e.g. TTSectionedDataSource) in a TTTableViewController
  • voila!

There is probably a more dynamic way to do this by implementing a TTTableViewDataSource subclass and letting it collect/manage the entities, but I don't think it's worth the effort.

(Prior to loading your entities onto your datasource in Three20, you need to convert them into table items, because a datasource is not exactly a datasource in Three20).

e.g.

[TTSectionedDataSource dataSourceWithObjects:
        @"",   // section header
        [TTTableTextItem itemWithText:@"An item" URL:@"http://www.facebook.com"],
        [TTTableSummaryItem itemWithText:@"Another item"],
        nil];

Update: I don't think that you can pass your entities directly to a detail view through a Three20 URL scheme (though there is a generic object mechanism). You can pass your object as part of an NSDictionary through the query parameter.

e.g. You can have a mapping such as

[map from:@"example://bookDetails/(initWithName:)" toViewController:[BookDetailsController class]];

and a method definition like this

- (id) initWithName:(NSString *)theName query:(NSDictionary *)query

You can use this to push the detail view controller

// navigationURLString = @"example://bookDatails/Alice in WonderLand" (in URL encoding)   
[self.navigationController pushViewController:[[TTNavigator navigator] 
                viewControllerForURL:navigationURLString query:dictionaryWithEntity] animated:YES];

Alternatively, you can pass the pertinent data through as arguments in the init call or just the entity's primary key and fetch the object again inside the detail view controller.

Upvotes: 1

Matt Long
Matt Long

Reputation: 24476

Core Data and Three20 won't have any specific implementation. They are tools that you can use to achieve the specific implementation yourself. You use Three20 to display the data you have in Core Data.

I suggest you dump Three20 and learn the basics of programming a standard table view controller first. You can see the very basics of a Core Data driven table view controller by simply creating a new iPhone app in Xcode and selecting "Use Core Data for storage". enter image description here

The template code will point you in the right direction at least and should help you grok MVC a bit better which will help you with a Three20 implementation. Meanwhile, you should also look into using mogenerator+xmo'd. It's the only way to fly when it comes to creating custom managed objects.

When in doubt, consult the master and ask specific Core Data questions here on SO.

Upvotes: 1

Related Questions