1. In your main class’ init method call this: operationQueue = [[NSOperationQueue alloc]init]; [operationQueue setMaxConcurrentOperationCount:1]; 2. Then in your viewWillAppear you can call this: [self showLoadingIndicators]; //calls a method which presents loading indicators (optional) [self beginLoadingTwitterData]; // this is the method that fires it all off 3. In your beginLoadingTwitterData you call this: NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(synchronousLoadTwitterData) object:nil]; [operationQueue addOperation:operation]; // creates and adds NSOperation to a queue [operation release]; 4. In this synchronousLoadTwitterData is where you do the heavy lifting: NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:[TwitterHelper fetchInfoForUsername:[twitterIds objectAtIndex:count]]] // this calls for a method “fetchInfoForUsername” which returns an NSDictionary, but to do so, it connects to… Read More
Continue ReadingNSOperation
Lets say you are not only downloading tweets (text) from the web, but actual chunks of raw data, such as images. You may have a method that returns a UIImage, such as: -(UIImage*)cachedImageForURL:(NSURL*)url{ //Preferably look for a cached image id cachedObject = [cachedImages objectForKey:url]; if (cachedObject == nil) { //set loading placeholder in our cache dict [cachedImages setObject:LoadingPlacedholder forKey:url]; //Create and queue a new image loading op ImageLoadingOp *operation = [[ImageLoadingOp alloc] initWithImageURL:url target:self action:@selector(didFinishLoadingImageWithResult:)]; [operationQueue addOperation:operation]; [operation release]; } else if(![cachedObject isKindOfClass:[UIImage class]]) { //were already loading the image, dont kick off another request cachedObject = nil; } return cachedObject; } Except notice this time the operation is not… Read More
Continue Reading