Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9148043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:09:56+00:00 2026-06-17T11:09:56+00:00

He there, i have a simple tree structure with files and folders. It’s part

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:09:57+00:00Added an answer on June 17, 2026 at 11:09 am

    Recursion already runs a “copy of the loop” for you:

    - (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:nil error:error];
            } else {
                [self loadFile:fileOrFolder.name
                        folder:folder.name
                      finished:^(NSData * data) {
                          ((DSFile *) fileOrFolder).data = data;
                      } error:error];
            }
        }
    }
    

    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 path a/b/c has a filename of c and a folder name of b, it would appear to load b/c.
    • error is called for every error. This is probably not what you want. It also does not return the load that caused the error.
    • It is more normal to have a single callback, e.g. void(^)(DSFolder*,NSError*).
    • finished is unused. It looks like you want it to be called when the entire hierarchy has finished loading, which is Slightly Tricky.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple file browser and there I display files and folders, obtained
I have a web application with simple tree structure of usergroups and looking for
I have a simple Crud application where there is one filed called Category. During
I have a simple vertical LinearLayout in which there is a button on the
I'm working on an application that generates a tree structure of nodes. There are
If i have a tree structure whose nodes can have zero to many children,
i have a simple question, there is a function with parameter emp_id that opens
I have a simple producer/consumer scenario, where there is only ever a single item
I have a simple setup, where there are Products and Images. Products have many
I have a simple feedback form in a Kentico CMS site. There are two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.