Querying a database using Cocoa
Before I start a disclaimer, I should add a little disclaimer, that I am relatively new to Cocoa development and C in general.
However I do have a copy of 'Cocoa Programming for Mac OS X 3rd edition' by Aaron Hillegass, which I am working through and using as a base, if anybody has a copy I am using the 'AmaZone' example on page 346 as a template and base.
I trying to develop a small client app that takes a search string then displays results from a database accordingly.
The database will contain a: list of files, their location, description & creation date, so for the moment the field number and types will remain the same.
After looking around on SO, I saw something like:
NSURL *myURL = [NSURL URLWithString:@"http://www.myserver.com/results.php"];
NSArray *sqlResults = [[NSArray alloc] initWithContentsOfURL:myURL];
I've worked with php before, so my current thinking after seeing this is create a php script on the server that queries the database, and creates an XML output.
And with the XML response, just parse it.
Would this be ok? as is there any major pitfalls anybody can see, that I can't.
I know there are some database bundles, I've had a look at BaseTen for Postgres, but being relatively new to this, didn't want to get in over my head.
Or if anybody else has any other suggestions a开发者_高级运维nd ideas, they would be greatly appreciated.
If your database is on a server, then this is a reasonable approach, and is exactly what Apple do with iTunes (only they use WebObjects, not php, for good enough reasons).
If the database is on your Mac OS X machine, then you should look at CoreData (also in Aaron's book, in a different chapter).
To improve your example, you should use asynchronous methods on NSURLConnection
, which will involve looking at the delegate protocols.
精彩评论