Skip to content

Instantly share code, notes, and snippets.

@js
Created February 12, 2013 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save js/4773884 to your computer and use it in GitHub Desktop.
Save js/4773884 to your computer and use it in GitHub Desktop.
Trying to wrap my head around ReactiveCocoa, by adding a repository list tableview to the GHAPIDemo sample project
// GHDUserViewController.m in the
// Take 1
- (id)initWithUserAccount:(GHGitHubUser *)user {
// .. snip ..
[[[RACSignal
merge:@[[self fetchUser], [self fetchRepos], [self fetchOrgs]]]
finally:^{
@strongify(self);
self.loading = NO;
}]
subscribeNext:^(id x) {
// nothing
} error:^(NSError *error) {
NSLog(@"error: %@", error);
} completed:^{
// this doesn't feel like Reactive approach, should probably react to
// self.userRepositories being changed (KVO style)?
[self.view.repositoriesTableView reloadData];
NSLog(@"done");
}];
// .. snip ..
}
// ...
- (RACSignal *)fetchRepos {
NSLog(@"Loading repos");
return [[self.client fetchUserRepos] map:^(NSArray *repositories) {
NSMutableArray *repositoryModels = [[NSMutableArray alloc] init];
for (NSDictionary *repoDict in repositories) {
GHGithubRepository *repo = [[GHGithubRepository alloc] initWithDictionary:repoDict];
[repositoryModels addObject:repo];
}
self.userRepositories = repositoryModels;
return [RACUnit defaultUnit];
}];
// Spontainously it feels like it might make more sense to yield my model objects directly from here,
// and then chain it with another signal to actually do with them as I please (eg assigning them to an
// array for the tableview).
// But the way I tried to do here seems overly verbose so not so sure that's the proper way either
// return [[[[self.client fetchUserRepos] sequence] map:^id(NSDictionary *reposDict) {
// return [[GHGithubRepository alloc] initWithDictionary:reposDict];
// }] signal];
}
////////////////////////////////
// take 2
- (id)initWithUserAccount:(GHGitHubUser *)user {
// ..snip..
RACSignal *fetchRepos = [[self.client fetchUserRepos] map:^id(NSArray *repos) {
NSMutableArray *repositoryModels = [[NSMutableArray alloc] init];
for (NSDictionary *repoDict in repos) {
GHGithubRepository *repo = [[GHGithubRepository alloc] initWithDictionary:repoDict];
[repositoryModels addObject:repo];
}
return repositoryModels;
}];
// This has the side-effect of fetching the repo list twice, guess I need to take a similar
// approach as -loadImageAtURL: ?
RAC(self.userRepositories) = fetchRepos;
[[[RACSignal
merge:@[[self fetchUser], fetchRepos, [self fetchOrgs]]]
finally:^{
@strongify(self);
self.loading = NO;
}]
subscribeNext:^(id x) {
// nothing
} error:^(NSError *error) {
NSLog(@"error: %@", error);
} completed:^{
// Again, this doesn't feel like the place to do this
[self.view.repositoriesTableView reloadData];
NSLog(@"done");
}];
// ... snip ..
}
@js
Copy link
Author

js commented Feb 12, 2013

I think what I'm struggling with here is mainly how to glue RAC together with the traditional cocoa world.

Specifically here, how to properly set up signals for getting updates for when the repo data arrives, pass that onto the array needed to display in the tableview, while still keeping the loading state (for the spinner).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment