Permalink
Browse files

Added new delegate method that is called whenever a connection is fin…

…ished -- makes final cleanup easier when there are multiple connections.
  • Loading branch information...
Craig Hockenberry
Craig Hockenberry committed Jan 21, 2009
1 parent a0447a1 commit 1f9476730ee3d14ca99449d0ff5edaef768810e5
Showing with 74 additions and 87 deletions.
  1. +53 −78 AppController.m
  2. +10 −0 MGTwitterEngine.m
  3. +11 −9 MGTwitterEngineDelegate.h
View
@@ -29,7 +29,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
[twitterEngine setUsername:username password:password];
// Get updates from people the authenticated user follows.
- //NSLog(@"getFollowedTimelineFor: connection identifier = %@", [twitterEngine getFollowedTimelineFor:nil since:nil startingAtPage:0]);
+ //NSLog(@"getFollowedTimelineFor: connectionIdentifier = %@", [twitterEngine getFollowedTimelineFor:nil since:nil startingAtPage:0]);
// Other types of information available from the API:
@@ -40,63 +40,63 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
// Status methods:
- NSLog(@"getPublicTimelineSinceID: connection identifier = %@", [twitterEngine getPublicTimelineSinceID:0]);
- //NSLog(@"getUserTimelineFor: connection identifier = %@", [twitterEngine getUserTimelineFor:TESTING_SECONDARY_USER sinceID:0 startingAtPage:0 count:20]);
- //NSLog(@"getUpdate: connection identifier = %@", [twitterEngine getUpdate:TESTING_ID]);
- //NSLog(@"sendUpdate: connection identifier = %@", [twitterEngine sendUpdate:[@"This is a test on " stringByAppendingString:[[NSDate date] description]]]);
- //NSLog(@"getRepliesStartingAtPage: connection identifier = %@", [twitterEngine getRepliesStartingAtPage:0]);
- //NSLog(@"deleteUpdate: connection identifier = %@", [twitterEngine deleteUpdate:TESTING_ID]);
+ NSLog(@"getPublicTimelineSinceID: connectionIdentifier = %@", [twitterEngine getPublicTimelineSinceID:0]);
+ //NSLog(@"getUserTimelineFor: connectionIdentifier = %@", [twitterEngine getUserTimelineFor:TESTING_SECONDARY_USER sinceID:0 startingAtPage:0 count:20]);
+ //NSLog(@"getUpdate: connectionIdentifier = %@", [twitterEngine getUpdate:TESTING_ID]);
+ //NSLog(@"sendUpdate: connectionIdentifier = %@", [twitterEngine sendUpdate:[@"This is a test on " stringByAppendingString:[[NSDate date] description]]]);
+ //NSLog(@"getRepliesStartingAtPage: connectionIdentifier = %@", [twitterEngine getRepliesStartingAtPage:0]);
+ //NSLog(@"deleteUpdate: connectionIdentifier = %@", [twitterEngine deleteUpdate:TESTING_ID]);
// User methods:
- //NSLog(@"getRecentlyUpdatedFriendsFor: connection identifier = %@", [twitterEngine getRecentlyUpdatedFriendsFor:nil startingAtPage:0]);
- //NSLog(@"getFollowersIncludingCurrentStatus: connection identifier = %@", [twitterEngine getFollowersIncludingCurrentStatus:YES]);
- //NSLog(@"getUserInformationFor: connection identifier = %@", [twitterEngine getUserInformationFor:TESTING_PRIMARY_USER]);
+ //NSLog(@"getRecentlyUpdatedFriendsFor: connectionIdentifier = %@", [twitterEngine getRecentlyUpdatedFriendsFor:nil startingAtPage:0]);
+ //NSLog(@"getFollowersIncludingCurrentStatus: connectionIdentifier = %@", [twitterEngine getFollowersIncludingCurrentStatus:YES]);
+ //NSLog(@"getUserInformationFor: connectionIdentifier = %@", [twitterEngine getUserInformationFor:TESTING_PRIMARY_USER]);
// Direct Message methods:
- //NSLog(@"getDirectMessagesSinceID: connection identifier = %@", [twitterEngine getDirectMessagesSinceID:0 startingAtPage:0]);
- //NSLog(@"getSentDirectMessagesSinceID: connection identifier = %@", [twitterEngine getSentDirectMessagesSinceID:0 startingAtPage:0]);
- //NSLog(@"sendDirectMessage: connection identifier = %@", [twitterEngine sendDirectMessage:[@"This is a test on " stringByAppendingString:[[NSDate date] description]] to:TESTING_SECONDARY_USER]);
- //NSLog(@"deleteDirectMessage: connection identifier = %@", [twitterEngine deleteDirectMessage:TESTING_MESSAGE_ID]);
+ //NSLog(@"getDirectMessagesSinceID: connectionIdentifier = %@", [twitterEngine getDirectMessagesSinceID:0 startingAtPage:0]);
+ //NSLog(@"getSentDirectMessagesSinceID: connectionIdentifier = %@", [twitterEngine getSentDirectMessagesSinceID:0 startingAtPage:0]);
+ //NSLog(@"sendDirectMessage: connectionIdentifier = %@", [twitterEngine sendDirectMessage:[@"This is a test on " stringByAppendingString:[[NSDate date] description]] to:TESTING_SECONDARY_USER]);
+ //NSLog(@"deleteDirectMessage: connectionIdentifier = %@", [twitterEngine deleteDirectMessage:TESTING_MESSAGE_ID]);
// Friendship methods:
- //NSLog(@"enableUpdatesFor: connection identifier = %@", [twitterEngine enableUpdatesFor:TESTING_SECONDARY_USER]);
- //NSLog(@"disableUpdatesFor: connection identifier = %@", [twitterEngine disableUpdatesFor:TESTING_SECONDARY_USER]);
- //NSLog(@"isUser:receivingUpdatesFor: connection identifier = %@", [twitterEngine isUser:TESTING_SECONDARY_USER receivingUpdatesFor:TESTING_PRIMARY_USER]);
+ //NSLog(@"enableUpdatesFor: connectionIdentifier = %@", [twitterEngine enableUpdatesFor:TESTING_SECONDARY_USER]);
+ //NSLog(@"disableUpdatesFor: connectionIdentifier = %@", [twitterEngine disableUpdatesFor:TESTING_SECONDARY_USER]);
+ //NSLog(@"isUser:receivingUpdatesFor: connectionIdentifier = %@", [twitterEngine isUser:TESTING_SECONDARY_USER receivingUpdatesFor:TESTING_PRIMARY_USER]);
// Account methods:
- //NSLog(@"checkUserCredentials: connection identifier = %@", [twitterEngine checkUserCredentials]);
- //NSLog(@"endUserSession: connection identifier = %@", [twitterEngine endUserSession]);
- //NSLog(@"setLocation: connection identifier = %@", [twitterEngine setLocation:@"Playing in Xcode with a location that is really long and may or may not get truncated to 30 characters"]);
- //NSLog(@"setNotificationsDeliveryMethod: connection identifier = %@", [twitterEngine setNotificationsDeliveryMethod:@"none"]);
+ //NSLog(@"checkUserCredentials: connectionIdentifier = %@", [twitterEngine checkUserCredentials]);
+ //NSLog(@"endUserSession: connectionIdentifier = %@", [twitterEngine endUserSession]);
+ //NSLog(@"setLocation: connectionIdentifier = %@", [twitterEngine setLocation:@"Playing in Xcode with a location that is really long and may or may not get truncated to 30 characters"]);
+ //NSLog(@"setNotificationsDeliveryMethod: connectionIdentifier = %@", [twitterEngine setNotificationsDeliveryMethod:@"none"]);
// TODO: Add: account/update_profile_colors
// TODO: Add: account/update_profile_image
// TODO: Add: account/update_profile_background_image
- //NSLog(@"getRateLimitStatus: connection identifier = %@", [twitterEngine getRateLimitStatus]);
+ //NSLog(@"getRateLimitStatus: connectionIdentifier = %@", [twitterEngine getRateLimitStatus]);
// TODO: Add: account/update_profile
// Favorite methods:
- //NSLog(@"getFavoriteUpdatesFor: connection identifier = %@", [twitterEngine getFavoriteUpdatesFor:nil startingAtPage:0]);
- //NSLog(@"markUpdate: connection identifier = %@", [twitterEngine markUpdate:TESTING_ID asFavorite:YES]);
+ //NSLog(@"getFavoriteUpdatesFor: connectionIdentifier = %@", [twitterEngine getFavoriteUpdatesFor:nil startingAtPage:0]);
+ //NSLog(@"markUpdate: connectionIdentifier = %@", [twitterEngine markUpdate:TESTING_ID asFavorite:YES]);
// Notification methods
- //NSLog(@"enableNotificationsFor: connection identifier = %@", [twitterEngine enableNotificationsFor:TESTING_SECONDARY_USER]);
- //NSLog(@"disableNotificationsFor: connection identifier = %@", [twitterEngine disableNotificationsFor:TESTING_SECONDARY_USER]);
+ //NSLog(@"enableNotificationsFor: connectionIdentifier = %@", [twitterEngine enableNotificationsFor:TESTING_SECONDARY_USER]);
+ //NSLog(@"disableNotificationsFor: connectionIdentifier = %@", [twitterEngine disableNotificationsFor:TESTING_SECONDARY_USER]);
// Block methods
- //NSLog(@"block: connection identifier = %@", [twitterEngine block:TESTING_SECONDARY_USER]);
- //NSLog(@"unblock: connection identifier = %@", [twitterEngine unblock:TESTING_SECONDARY_USER]);
+ //NSLog(@"block: connectionIdentifier = %@", [twitterEngine block:TESTING_SECONDARY_USER]);
+ //NSLog(@"unblock: connectionIdentifier = %@", [twitterEngine unblock:TESTING_SECONDARY_USER]);
// Help methods:
- //NSLog(@"testService: connection identifier = %@", [twitterEngine testService]);
+ //NSLog(@"testService: connectionIdentifier = %@", [twitterEngine testService]);
#if YAJL_AVAILABLE
// Search method
- //NSLog(@"getSearchResultsForQuery: connection identifier = %@", [twitterEngine getSearchResultsForQuery:TESTING_PRIMARY_USER sinceID:0 startingAtPage:1 count:20]);
+ //NSLog(@"getSearchResultsForQuery: connectionIdentifier = %@", [twitterEngine getSearchResultsForQuery:TESTING_PRIMARY_USER sinceID:0 startingAtPage:1 count:20]);
// Trends method
- //NSLog(@"getTrends: connection identifier = %@", [twitterEngine getTrends]);
+ //NSLog(@"getTrends: connectionIdentifier = %@", [twitterEngine getTrends]);
#endif
}
@@ -110,90 +110,65 @@ - (void)dealloc
#pragma mark MGTwitterEngineDelegate methods
-- (void)requestSucceeded:(NSString *)requestIdentifier
+- (void)requestSucceeded:(NSString *)connectionIdentifier
{
- NSLog(@"Request succeeded for connection identifier = %@", requestIdentifier);
+ NSLog(@"Request succeeded for connectionIdentifier = %@", connectionIdentifier);
}
-- (void)requestFailed:(NSString *)requestIdentifier withError:(NSError *)error
+- (void)requestFailed:(NSString *)connectionIdentifier withError:(NSError *)error
{
- NSLog(@"Request failed for connection identifier = %@, error = %@ (%@)",
- requestIdentifier,
+ NSLog(@"Request failed for connectionIdentifier = %@, error = %@ (%@)",
+ connectionIdentifier,
[error localizedDescription],
[error userInfo]);
}
-- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)identifier
+- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier
{
- NSLog(@"Got statuses for %@:\r%@", identifier, statuses);
-
- if ([twitterEngine numberOfConnections] == 1)
- {
- [NSApp terminate:self];
- }
+ NSLog(@"Got statuses for %@:\r%@", connectionIdentifier, statuses);
}
-- (void)directMessagesReceived:(NSArray *)messages forRequest:(NSString *)identifier
+- (void)directMessagesReceived:(NSArray *)messages forRequest:(NSString *)connectionIdentifier
{
- NSLog(@"Got direct messages for %@:\r%@", identifier, messages);
-
- if ([twitterEngine numberOfConnections] == 1)
- {
- [NSApp terminate:self];
- }
+ NSLog(@"Got direct messages for %@:\r%@", connectionIdentifier, messages);
}
-- (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)identifier
+- (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)connectionIdentifier
{
- NSLog(@"Got user info for %@:\r%@", identifier, userInfo);
-
- if ([twitterEngine numberOfConnections] == 1)
- {
- [NSApp terminate:self];
- }
+ NSLog(@"Got user info for %@:\r%@", connectionIdentifier, userInfo);
}
-- (void)miscInfoReceived:(NSArray *)miscInfo forRequest:(NSString *)identifier
+- (void)miscInfoReceived:(NSArray *)miscInfo forRequest:(NSString *)connectionIdentifier
{
- NSLog(@"Got misc info for %@:\r%@", identifier, miscInfo);
-
- if ([twitterEngine numberOfConnections] == 1)
- {
- [NSApp terminate:self];
- }
+ NSLog(@"Got misc info for %@:\r%@", connectionIdentifier, miscInfo);
}
-- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)identifier
+- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier
{
- NSLog(@"Got search results for %@:\r%@", identifier, searchResults);
-
- if ([twitterEngine numberOfConnections] == 1)
- {
- [NSApp terminate:self];
- }
+ NSLog(@"Got search results for %@:\r%@", connectionIdentifier, searchResults);
}
-- (void)imageReceived:(NSImage *)image forRequest:(NSString *)identifier
+- (void)imageReceived:(NSImage *)image forRequest:(NSString *)connectionIdentifier
{
- NSLog(@"Got an image for %@: %@", identifier, image);
+ NSLog(@"Got an image for %@: %@", connectionIdentifier, image);
// Save image to the Desktop.
- NSString *path = [[NSString stringWithFormat:@"~/Desktop/%@.tiff", identifier]
- stringByExpandingTildeInPath];
+ NSString *path = [[NSString stringWithFormat:@"~/Desktop/%@.tiff", connectionIdentifier] stringByExpandingTildeInPath];
[[image TIFFRepresentation] writeToFile:path atomically:NO];
+}
-
- if ([twitterEngine numberOfConnections] == 1)
+- (void)connectionFinished
+{
+ if ([twitterEngine numberOfConnections] == 0)
{
[NSApp terminate:self];
}
}
-
@end
View
@@ -310,6 +310,8 @@ - (void)closeConnection:(NSString *)identifier
if (connection) {
[connection cancel];
[_connections removeObjectForKey:identifier];
+ if ([self _isValidDelegateForSelector:@selector(connectionFinished)])
+ [_delegate connectionFinished];
}
}
@@ -760,6 +762,8 @@ - (void)connection:(MGTwitterHTTPURLConnection *)connection didReceiveResponse:(
// Destroy the connection.
[connection cancel];
[_connections removeObjectForKey:[connection identifier]];
+ if ([self _isValidDelegateForSelector:@selector(connectionFinished)])
+ [_delegate connectionFinished];
} else if (statusCode == 304 || [connection responseType] == MGTwitterGeneric) {
// Not modified, or generic success.
@@ -774,6 +778,8 @@ - (void)connection:(MGTwitterHTTPURLConnection *)connection didReceiveResponse:(
// Destroy the connection.
[connection cancel];
[_connections removeObjectForKey:[connection identifier]];
+ if ([self _isValidDelegateForSelector:@selector(connectionFinished)])
+ [_delegate connectionFinished];
}
if (NO) {
@@ -802,6 +808,8 @@ - (void)connection:(MGTwitterHTTPURLConnection *)connection didFailWithError:(NS
// Release the connection.
[_connections removeObjectForKey:[connection identifier]];
+ if ([self _isValidDelegateForSelector:@selector(connectionFinished)])
+ [_delegate connectionFinished];
}
@@ -845,6 +853,8 @@ - (void)connectionDidFinishLoading:(MGTwitterHTTPURLConnection *)connection
// Release the connection.
[_connections removeObjectForKey:[connection identifier]];
+ if ([self _isValidDelegateForSelector:@selector(connectionFinished)])
+ [_delegate connectionFinished];
}
View
@@ -10,21 +10,23 @@
@protocol MGTwitterEngineDelegate
-- (void)requestSucceeded:(NSString *)requestIdentifier;
-- (void)requestFailed:(NSString *)requestIdentifier withError:(NSError *)error;
+- (void)requestSucceeded:(NSString *)connectionIdentifier;
+- (void)requestFailed:(NSString *)connectionIdentifier withError:(NSError *)error;
-- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)identifier;
-- (void)directMessagesReceived:(NSArray *)messages forRequest:(NSString *)identifier;
-- (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)identifier;
-- (void)miscInfoReceived:(NSArray *)miscInfo forRequest:(NSString *)identifier;
+- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier;
+- (void)directMessagesReceived:(NSArray *)messages forRequest:(NSString *)connectionIdentifier;
+- (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)connectionIdentifier;
+- (void)miscInfoReceived:(NSArray *)miscInfo forRequest:(NSString *)connectionIdentifier;
#if YAJL_AVAILABLE
-- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)identifier;
+- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier;
#endif
#if TARGET_OS_IPHONE
-- (void)imageReceived:(UIImage *)image forRequest:(NSString *)identifier;
+- (void)imageReceived:(UIImage *)image forRequest:(NSString *)connectionIdentifier;
#else
-- (void)imageReceived:(NSImage *)image forRequest:(NSString *)identifier;
+- (void)imageReceived:(NSImage *)image forRequest:(NSString *)connectionIdentifier;
#endif
+- (void)connectionFinished;
+
@end

0 comments on commit 1f94767

Please sign in to comment.