我正在尝试使用 Octokit 预取 Github 存储库中文件的对象图这取决于 Reactive Cococa .我遇到了一个问题,它创建了一个信号,该信号将递归地向下钻取,直到没有更多的目录要获取为止。这是 repository of mine 的示例目录图(注意:文件已被省略,以保持图表简单明了)。
- (RACSignal *)fetchContentTreeForRepository:(OCTRepository *)repository {
return [[self fetchContent:Nil forRepository:repository parentContent:nil] doCompleted:^{
// fetching tree finished, persist in database
}];
}
- (RACSignal *)fetchContent:(OCTContent *)content forRepository:(OCTRepository *)repository parentContent:(OCTContent *)parentContent {
return [[[[self.client fetchContents:content forRepository:repository] collect] deliverOn:RACScheduler.mainThreadScheduler]
flattenMap:^RACSignal *(NSArray *fetchedContents) {
// set the contents that were fetched
id<OCTContentStoreProtocol>store = content ?: repository;
store.contents = fetchedContents;
// only search for contents of type "dir" (directory)
NSArray *directories = [fetchedContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"contentType = \"dir\""]];
NSMutableArray *signals;
for (OCTContent *fetchedDir in directories) {
[signals addObject:[self fetchContent:fetchedDir forRepository:repository parentContent:content]];
}
return [RACSignal merge:signals];
}];
}
请注意:-fetchContents:forRepository:
构建一个请求路径并返回一个 RACSignal
将 HTTP 请求操作排队(它试图遵循语义 OctoKit does ).
我目前遇到的问题是此设置仅执行存储库内容的提取(即图中的顶级对象)。 -flattenMap:
被调用,信号数组被适本地创建,并且 -merge:
被返回。目的是创建一个递归链,当没有更多的目录类型子级时结束(可能应该添加一个 -filter:
来检查)。
目的是获取 Github 存储库的整个文件图,并在操作完成时收到通知。这是我想如何处理调用的示例:
[[[GHDataStore sharedStore] fetchContentTreeForRepository:self.repository]
subscribeNext:^(NSArray *contents) {
// this would be called each time a new set of contents is received
NSLog(@"received %i contents",[contents count]);
}
error:^(NSError *error) {
NSLog(@"error fetching: %@",error.localizedDescription);
}
completed:^{
// this would be called when mapping the graph is finished
NSLog(@"finished fetching contents");
}];
知道为什么它只执行顶层吗?我原以为在 -fetchContentTreeForRepository:
上调用 -subscribeNext:
会执行 -flattenMap:
的返回信号,但看来我误会了什么。这个假设来自 chaining example在 Reactive Cocoa 自述文件中。
编辑:I am dumb.
最佳答案
问题是您从未初始化信号的 NSMutableArray
:
NSMutableArray *signals;
for (OCTContent *fetchedDir in directories) {
[signals addObject:[self fetchContent:fetchedDir forRepository:repository parentContent:content]];
}
return [RACSignal merge:signals];
将第一行更改为 NSMutableArray *signals = [NSMutableArray new]
它似乎可以工作。
关于objective-c - 递归-flattenMap : with Reactive Cocoa + OctoKit (fetching dynamic object graph from web service),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17408477/