He there, i have a simple tree structure with files and folders. It’s part of a client- server application which can download virtual folders. Anyways i like using ^blocks for async programming. I have 3 urls, the first one downloads a folder description, the second one is for downloading a file and the third one is for downloading a folder contents. DSFolder is the folder description and DSFile inherits from DSFolder and contains a NSData field for the download result. The folder downloading code looks like this:
- (void)loadFolderData:(DSFolder *)folder
finished:(void(^)(DSFolder *))finished
error:(void(^)(NSError *))error {
if (!folder) {
return;
}
for (DSFolder* fileOrFolder in folder.children) {
if ([fileOrFolder isFolder]) {
[self loadFolderData:fileOrFolder
finished:^(DSFolder * folder) {
// *********************************
// Next folder level would be a copy
// of the whole for loop in here
// *********************************
} error:^(NSError * err) {
if (error) {
error(err);
}
}];
} else {
[self loadFile:fileOrFolder.name
folder:folder.name
finished:^(NSData * data) {
((DSFile *) fileOrFolder).data = data;
} error:^(NSError * err) {
if (error) {
error(err);
}
}];
}
}
}
Well i could limit the tree level depth by just copying the loop inside a few times, but that seems very ugly. I hope this question is reasonable.
Recursion already runs a “copy of the loop” for you:
I’m assuming that
-loadFile:finished:error:correctly handles a nil error-block.A few other things:
[self loadFile:fileOrFolder.name folder:folder.name ...]looks like it does the wrong thing if there are subdirectories: Assuming the patha/b/chas a filename ofcand a folder name ofb, it would appear to loadb/c.erroris called for every error. This is probably not what you want. It also does not return the load that caused the error.void(^)(DSFolder*,NSError*).finishedis unused. It looks like you want it to be called when the entire hierarchy has finished loading, which is Slightly Tricky.