Search posterous

Search all posts and users. Type a name, type a favorite song title, whatever! See what comes up.
  

More posterous blogs











More recommended blogs »

Here are posterous posts filed under cocoa...

Dubber says...

Filed under: Cocoa

Archimage says...

So in the last two posts I dealt with setting up CoreData within an existing project, binding the data (NSManagedObjects to a control) and actually getting the objects to save to a file (store).  In this last entry, I'll deal with pulling data from the file in order to display it in the control.   This code assumes you have an NSManagedContext I'm calling newContext.

NSError **theError = nil;  // set up an error code

// We need to use an NSEntityDescription that the docs describe as being an abstract form of NSManagedObject. (It's like "id" is to classes.)  
// When we create an NSEntityDescription we need to map it (think cast it) to the entity in our data model we want to read within our context.
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Recent" inManagedObjectContext:newContext];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];  // Create a CoreData fetch request (NSFetchRequest)
[request setEntity:entityDescription];  // tell the fetch request what type of entity we want to fetch (using our NSEntityDescription)

 

 

 


// Here we need to specify our NSPredicate (think SQL SELECT predicate) to specify the subset of objects to read from our store.
// The bad news is that CoreData predicates don't use SQL syntax, which is a shame.  They use their own special syntax which makes learning a bit more problematic
// and difficult.
// I am keeping things simple here and am going to assume if I don't specify a predicate CoreData will do the sensible thing and return all objects.
// (I am just guessing here, since I didn't feel like wading thru the NSPredicate Programming Manual.)
// NSPredicate *predicate = [NSPredicate predicateWithFormat:...];
// [request setPredicate:predicate];

 

 

 


// Next, we create an NSSortDescriptor which is used to sort the object during the fetch (think an SQL ORDER BY clause.)

// Specify the property name within the entity we want to sort by (I'm using gitName) and the sort order.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor allocinitWithKey:@"gitName" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];  // tell the NSFetchRequest to use the sort descriptor
[sortDescriptor release]; // clean up

 

 

 


NSArray *array = [newContext executeFetchRequest:request error:theError]; // create an array with the results of executing the fetch request on our context.
if (array != nil)
{
// move the results to our array of objects which are bound to our control.
}
else
{
// Deal with error...
}
[array release];

That's it--it works!

So, what have I learned?  Once things are set up CoreData isn't bad or overly difficult to use in this simplest of approaches. The code I've presented is pretty brute-force, but it works.  The complexity as I see it comes in learning how to manage the various layers of the graph:  the persistent store, the context, the managed objects themselves, and probably the hardest layer, the predicates.  There are some analogs to what and how SQL works, but its not as simple as SQL.  SQL is declarative and purely syntax driven.  CoreData is obviously object-oriented, but its more complex than an ORM (Object-Relational Mapping) tool such as hibernate.  

Am I going to keep using CoreData?  Yes, mainly because I got the basics working, it gives me a level of confidence, and I know I can pick up the more complex options as I need them bit by bit, and I personally have no need to learn all of CoreData at once.  

Is CoreData more cumbersome and complex than it has to be? Probably.  

Filed under: Cocoa, CoreData, database, Mac, SQL

Archimage says...

Continuing on my quick CoreData excursion, here is what I figured out and learned today  regarding actually getting your NSManagedObjects to save to a real file.   This assumes the previous post's code.

Saving your objects to the file/store is pretty straight forward.  Again, I'm just trying to get basic functionality working.   Once I have that done I can expand on my knowledge and what I try to code after I understand what is going on. 

Saving is pretty straight forward once you have the setup code done.  In the previous entry's code example, there is a comment you need to remove and implement:

//- (NSPersistentStore *)addPersistentStoreWithType:(NSString *)storeType configuration:nil URL:(NSURL *)storeURL options:(NSDictionary *)options error:(NSError **)error     // this is what you use to set up the database type, etc.  In this snippet I don't worry about it because I just want it to compile and display something.

This is my brute force code:
NSError *error;  

// Build an NSURL to our physical database file.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *folder = @"~/Library/Application Support/Tiny±Git/";
folder = [folder stringByExpandingTildeInPath];
if ([fileManager fileExistsAtPath: folder] == NO)
{
[fileManager createDirectoryAtPath:folder withIntermediateDirectories:NO attributes:nil error:error];
}

    

NSURL *fileURL  = [NSURL fileURLWithPath: [folder stringByAppendingPathComponent: @"TinyGit.sqlite"]];

// Now tell our NSPersistanceStoreCoordinator the type of DB you want (I'm using SQLite, you can also use XML, etc.) 
// Pass in our fileURL NSURL which is where the database will be strored. 
// There are also configurations, and options, but we can ignore this--I'll worry about those once I get this working.
[psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:fileURL options:nil error:error];

Once you have the store coordinator set up this way, then all you have to do to store your NSManagedObject(s) is call:

[newContext save:error];

after you have created them and added them to the NSManagedObjectContext.  The interesting thing here is that the the save: actually forces all the objects within the context to be saved all at once.  It's all or nothing.  So make sure you only add the objects to the context that you want to end up in the database.

Just to verify that this actually did work, I went to my Application Support folder and sure enough there was a new Tiny±Git folder and within it a new file TinyGit.sqlite.  Just to see what got stored (if anything) I opened it up with SQLiteManager:

Compare this with the actual data model:

So, it looks like CoreData builds lots of infrastructure.  Just to see what data got stored I drilled into the data:

Well at least the data that I specified for my simple example in the previous post got saved properly, but there sure is a lot of extraneous stuff going on under the covers.

What do I conclude from all this?  I honestly am not sure. I'm feeling a bit more confident after seeing how relatively easy it is (although limiting) to save a context.  I'll have to see what it's like to actually retrieve/fetch data using CoreData.

Filed under: Cocoa, CoreData, database, Macintosh, SQL

thefabawards says...

Cadbury and Ghana celebrate Fairtrade pact

If you think the drumming gorilla or dancing-eyebrows kids got big heads from starring in Cadbury ads, check out the oversized star of "Zingolo," this new Cadbury video, shot in Ghana. The song is the first single off an album from the firm's Glass and a Half Full Productions celebrating African music and culture, while touting the U.K. confectioner's switch to Fairtrade cocoa. (Cadbury will pay a guaranteed minimum price for Ghanaian cocoa under a £45 million initiative over 10 years.) The song is infectious, and we'll assume Cadbury doesn't really use exploding, psychedelic cocoa beans in its chocolate bars (though it would explain why I feel high after a few bites). Best of all, there's no Phil Collins on the soundtrack.

—Posted by David Gianatasio

Filed under: Advertising, Cadbury, Cocoa, Confectionery & Snacks, Ghana, September 2009, Television

chak says...

Here is the language specification for blocks (that is lambda abstractions) for C, Objective-C & C++ as implemented in the Clang compiler and used in Mac OS X 10.6 (Snow Leopard):

   http://clang.llvm.org/docs/BlockLanguageSpec.txt

Filed under: cocoa, mac, objc

mickes says...

Filed under: brown, cocoa, hot

jsmp says...

Here are some of the resources I find interesting, this is not meant to be a extended version but just a quick post to play with posterous.

As an Objective-C/Cocoa Developer:

As an designer/artist
As for electronics

Have fun.

Filed under: art, cocoa, electronics

obleo says...

A Stanford computer science class is available to the general public free online. The full 10 week course includes videos and pdf's. Online viewers of the Stanford course will see the same lectures as the on-campus students, but will not receive credit for the course.

23 videos around a hour each and 17 pdf docs.

see it, download it (with iTunes) @ http://deimos3.apple.com/WebObjects/Core.woa/Browse/itunes.stanford.edu.2024353965.02024353968

Filed under: Apple, cocoa, development, free course, interface builder, iPhone, iphone development, object-c, Stanford U, video, xcode

joehickman says...

I'm going to Train a monkey to bring me 8 alphabet blocks and when each time he brings me 8, I will give him a bannana. Each time I will use the blocks in order he gave them to me as my password. Then, when my clients call in and tell me their password got hacked and ask me "What the F, do you guys have monkeys running that place or what?" Yes, yes we do, asshole.

Thanks Austin...

 

Filed under: Austin Tate, Client Passwords, Clients, Cocoa, Joe Hickman, Michael Jackson, Password Generator, Password Unlocker, Train Monkeys, Trained Money, Website Passwords

John says...

There are some great books out there to help you learn the iPhone SDK, Objective-C and Cocoa.  Here's a link to a few of the best: Link

Filed under: book, book review, cocoa, iphone, iphonedev, objective-c