Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter6: update code for requesting friends facebook profile picture…
…s. add facebook requests to a queue
- Loading branch information
1 parent
c37e409
commit 3f4522b
Showing
8 changed files
with
275 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// FacebookRequestController.h | ||
// ApiFacebook | ||
// | ||
// Created by Christopher White on 5/12/11. | ||
// Copyright 2011 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "Facebook.h" | ||
|
||
@interface FacebookRequestController : NSObject <FBRequestDelegate> { | ||
NSDictionary *currentRequestDictionary; | ||
NSMutableArray *requestQueue; | ||
BOOL handlingRequest; | ||
} | ||
|
||
@property(nonatomic, retain) NSDictionary *currentRequestDictionary; | ||
|
||
+ (FacebookRequestController*)sharedRequestController; | ||
- (void)enqueueRequestWithGraphPath:(NSString*)path; | ||
|
||
@end |
147 changes: 147 additions & 0 deletions
147
Chapter6/ApiFacebook/Classes/FacebookRequestController.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// | ||
// FacebookRequestController.m | ||
// ApiFacebook | ||
// | ||
// Created by Christopher White on 5/12/11. | ||
// Copyright 2011 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#import "FacebookRequestController.h" | ||
#import "NSMutableArray+QueueAdditions.h" | ||
#import "AppDelegate.h" | ||
|
||
static FacebookRequestController *sharedRequestController; | ||
|
||
@implementation FacebookRequestController | ||
|
||
@synthesize currentRequestDictionary; | ||
|
||
+ (FacebookRequestController*)sharedRequestController | ||
{ | ||
if (nil == sharedRequestController) { | ||
sharedRequestController = [[FacebookRequestController alloc] init]; | ||
} | ||
|
||
return sharedRequestController; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
[currentRequestDictionary release]; | ||
|
||
[requestQueue release]; | ||
|
||
[super dealloc]; | ||
} | ||
|
||
//do one request at a time...monitor the queue on a separate thread... | ||
//check the queue on a separate thread and if the queue is not empty, then take the next one off of the queue and issue a request for it on the main thread | ||
|
||
- (void)issueRequest:(NSString*)path | ||
{ | ||
[facebook requestWithGraphPath:path andDelegate:self]; | ||
} | ||
|
||
- (void)dequeueNextRequest | ||
{ | ||
//only want to dequeue the next request if we're not currently processing a request | ||
|
||
NSString *currentPath = nil; | ||
|
||
if (NO == handlingRequest) { | ||
@synchronized(self) { | ||
if (0 < [requestQueue count]) { | ||
currentPath = [requestQueue dequeue]; | ||
} | ||
} | ||
|
||
if (currentPath) { | ||
handlingRequest = YES; | ||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; | ||
[dictionary setObject:currentPath forKey:@"path"]; | ||
|
||
self.currentRequestDictionary = dictionary; | ||
|
||
[self performSelectorOnMainThread:@selector(issueRequest:) withObject:currentPath waitUntilDone:NO]; | ||
} | ||
} | ||
} | ||
|
||
- (void)processQueue:(id)object | ||
{ | ||
while (TRUE) { | ||
[[FacebookRequestController sharedRequestController] dequeueNextRequest]; | ||
|
||
[NSThread sleepForTimeInterval:.01]; | ||
} | ||
} | ||
|
||
- (void)enqueueRequestWithGraphPath:(NSString*)path | ||
{ | ||
if (nil == requestQueue) { | ||
requestQueue = [[NSMutableArray array] retain]; | ||
|
||
[self performSelectorInBackground:@selector(processQueue:) withObject:nil]; | ||
} | ||
|
||
@synchronized(self) { | ||
[requestQueue enqueue:path]; | ||
} | ||
} | ||
|
||
#pragma mark - | ||
#pragma mark FBRequestDelegate | ||
|
||
- (void)requestLoading:(FBRequest *)request { | ||
NSLog(@"requestLoading:"); | ||
} | ||
|
||
/** | ||
* Called when the server responds and begins to send back data. | ||
*/ | ||
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response { | ||
NSLog(@"didReceiveResponse:"); | ||
} | ||
|
||
/** | ||
* Called when an error prevents the request from completing successfully. | ||
*/ | ||
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error { | ||
NSLog(@"didFailWithError:"); | ||
|
||
handlingRequest = NO; | ||
} | ||
|
||
/** | ||
* Called when a request returns and its response has been parsed into an object. | ||
* | ||
* The resulting object may be a dictionary, an array, a string, or a number, depending | ||
* on thee format of the API response. | ||
*/ | ||
- (void)request:(FBRequest *)request didLoad:(id)result { | ||
NSLog(@"didLoad:"); | ||
|
||
//post a notification with the current dictionary | ||
//issue notification to whoever registered for the given path and include the result response | ||
|
||
handlingRequest = NO; | ||
|
||
NSDictionary *userInfoDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:result, nil] forKeys:[NSArray arrayWithObjects:@"result", nil]]; | ||
|
||
NSString *path = [self.currentRequestDictionary objectForKey:@"path"]; | ||
[[NSNotificationCenter defaultCenter] postNotificationName:path | ||
object:self | ||
userInfo:userInfoDictionary]; | ||
} | ||
|
||
/** | ||
* Called when a request returns a response. | ||
* | ||
* The result object is the raw response from the server of type NSData | ||
*/ | ||
- (void)request:(FBRequest *)request didLoadRawResponse:(NSData *)data { | ||
NSLog(@"didLoadRawResponse:"); | ||
} | ||
|
||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Chapter6/ApiFacebook/Classes/NSMutableArray+QueueAdditions.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// NSMutableArray+QueueAdditions.h | ||
// ApiFacebook | ||
// | ||
// Created by Christopher White on 5/12/11. | ||
// Copyright 2011 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
|
||
@interface NSMutableArray (QueueAdditions) | ||
- (id) dequeue; | ||
- (void) enqueue:(id)obj; | ||
@end | ||
|
32 changes: 32 additions & 0 deletions
32
Chapter6/ApiFacebook/Classes/NSMutableArray+QueueAdditions.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// NSMutableArray+QueueAdditions.m | ||
// ApiFacebook | ||
// | ||
// Created by Christopher White on 5/12/11. | ||
// Copyright 2011 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#import "NSMutableArray+QueueAdditions.h" | ||
|
||
|
||
@implementation NSMutableArray (QueueAdditions) | ||
|
||
- (id)dequeue | ||
{ | ||
if ([self count] == 0) { | ||
return nil; | ||
} | ||
id queueObject = [[[self objectAtIndex:0] retain] autorelease]; | ||
[self removeObjectAtIndex:0]; | ||
return queueObject; | ||
} | ||
|
||
|
||
// Add to the tail of the queue (no one likes it when people cut in line!) | ||
- (void) enqueue:(id)anObject | ||
{ | ||
[self addObject:anObject]; | ||
//this method automatically adds to the end of the array | ||
} | ||
@end | ||
|