Permalink
Browse files

Having a repository action that encompasses multiple requests revealed

an issue with using the resource delegate to send repository fetch start/finish
notifications.

If a repository fires off multiple operations, before the first finishes,
when one does finish it will fire the didFinish repository delegate method
before its actually completed.

So, whilst not quite as convenient, I've added an API for indicating the
start and finish of an operation and left it to the individual repository
methods to indicate when they have started and finished.
  • Loading branch information...
1 parent 812644f commit 4b039e7bf39938393caa1b6759189fba65f55557 @lukeredpath lukeredpath committed Aug 6, 2010
@@ -34,8 +34,8 @@ typedef void (^GithubUserRepositoryResultBlock)(GithubUser *user);
@protocol RemoteResourceRepositoryDelegate <NSObject>
@optional
-- (void)repositoryWillFetchFromResource:(RemoteResourceRepository *)repository;
-- (void)repositoryDidFetchFromResource:(RemoteResourceRepository *)repository;
+- (void)repositoryDidStartRemoteOperation:(RemoteResourceRepository *)repository;
+- (void)repositoryDidFinishRemoteOperation:(RemoteResourceRepository *)repository;
@end
@class LRRestyResource;
@@ -53,6 +53,11 @@ - (void)setFollowers:(NSArray *)replacementFollowers
@end
+@interface RemoteResourceRepository ()
+- (void)startRemoteOperation;
+- (void)finishRemoteOperation;
+@end
+
@implementation RemoteResourceRepository
@synthesize delegate;
@@ -61,7 +66,6 @@ - (id)initWithRemoteResource:(LRRestyResource *)aResource;
{
if (self = [super init]) {
resource = [aResource retain];
- resource.delegate = self;
}
return self;
}
@@ -72,14 +76,18 @@ - (void)dealloc
[super dealloc];
}
-- (void)resourceWillPerformRequest:(LRRestyResource *)resource;
+- (void)startRemoteOperation
{
- [self.delegate repositoryWillFetchFromResource:self];
+ if ([self.delegate respondsToSelector:@selector(repositoryDidStartRemoteOperation:)]) {
+ [self.delegate repositoryDidStartRemoteOperation:self];
+ }
}
-- (void)resourceDidPerformRequest:(LRRestyResource *)resource;
+- (void)finishRemoteOperation
{
- [self.delegate repositoryDidFetchFromResource:self];
+ if ([self.delegate respondsToSelector:@selector(repositoryDidFinishRemoteOperation:)]) {
+ [self.delegate repositoryDidFinishRemoteOperation:self];
+ }
}
@end
@@ -95,6 +103,8 @@ GithubID userIDFromString(NSString *userIDString)
- (void)getUserWithUsername:(NSString *)username
andYield:(GithubUserRepositoryResultBlock)resultBlock;
{
+ [self startRemoteOperation];
+
[[resource at:[NSString stringWithFormat:@"user/show/%@", username]] get:^(LRRestyResponse *response, LRRestyResource *userResource) {
NSDictionary *userData = [[response asJSONObject] objectForKey:@"user"];
@@ -104,21 +114,27 @@ - (void)getUserWithUsername:(NSString *)username
[[userResource at:@"followers"] get:^(LRRestyResponse *response, LRRestyResource *followersResource) {
[user setFollowers:[[response asJSONObject] objectForKey:@"users"]];
resultBlock(user);
+
[user release];
+ [self finishRemoteOperation];
}];
}];
}
- (void)getUsersMatching:(NSString *)searchString
andYield:(RepositoryCollectionResultBlock)resultBlock;
{
+ [self startRemoteOperation];
+
[[resource at:[NSString stringWithFormat:@"user/search/%@", searchString]] get:^(LRRestyResponse *response, LRRestyResource *userResource) {
NSMutableArray *users = [NSMutableArray array];
for (NSDictionary *userData in [[response asJSONObject] objectForKey:@"users"]) {
GithubUser *user = [[GithubUser alloc] initWithUsername:[userData objectForKey:@"username"] remoteID:userIDFromString([userData objectForKey:@"id"])];
user.fullName = [userData objectForKey:@"fullname"];
[users addObject:user];
+
[user release];
+ [self finishRemoteOperation];
}
resultBlock(users);
}];
@@ -138,12 +138,12 @@ - (void)pushUserListWithUsers:(NSArray *)users;
#pragma mark RemoteResourceRepositoryDelegate methods
-- (void)repositoryWillFetchFromResource:(RemoteResourceRepository *)repository
+- (void)repositoryDidStartRemoteOperation:(RemoteResourceRepository *)repository
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
-- (void)repositoryDidFetchFromResource:(RemoteResourceRepository *)repository
+- (void)repositoryDidFinishRemoteOperation:(RemoteResourceRepository *)repository
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

0 comments on commit 4b039e7

Please sign in to comment.