Skip to content
This repository was archived by the owner on Apr 25, 2018. It is now read-only.

How to Use the API

brockwoolf edited this page Sep 13, 2010 · 12 revisions

Sample Code

Using StackKit relies upon creating an SKSite object:
SKSite * stackOverflow = [[SKSite alloc] initWithAPIURL:[NSURL URLWithString:@"http://api.stackoverflow.com"] APIKey:@"my-super-secret-api-key"];

Requesting information can be done via an SKFetchRequest object (nearly identical to an NSFetchRequest):
SKFetchRequest * request = [[SKFetchRequest alloc] init]; //The kind of object you want to request: [request setEntity:[SKUser class]]; //The predicate to limit the results. This particular predicate will fetch one user with the id "115730" [request setPredicate:[NSPredicate predicateWithFormat:@"%K = %d", SKUserID, 115730]];

Once you’ve created your fetch request, there are two ways you can execute it: synchronously (which will block the calling thread) or asynchronously (which won’t block the thread).

Synchronous:
NSError * error = nil; NSArray * results = [stackOverflow executeSynchronousFetchRequest:request error:&error];
If the request was successful, then results will contain one or more SKUser objects (depending on the predicate). If there was an error with the request, then the NSError object will be filled in with appropriate information and results will be nil.

Asynchronous:
[request setDelegate:self]; //self must conform to the <SKFetchRequestDelegate> protocol [stackOverflow executeFetchRequest:request];
When the request finishes, it will invoke one of:
- (void) fetchRequest:(SKFetchRequest *)request didReturnResults:(NSArray *)results; - (void) fetchRequest:(SKFetchRequest *)request didFailWithError:(NSError *)error;

Clone this wiki locally