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 9132755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:22:06+00:00 2026-06-17T08:22:06+00:00

In my current design I have a tableview which represents a folder structure. When

  • 0

In my current design I have a tableview which represents a folder structure. When the user taps on a cell an AFHTTPRequestOperation is created to download a file. The file is downloaded while the cell currently shows the current download state: none/downloaded/downloading.

The download state is set on an NSManagedObject which corresponds to each cell. In my completion block for the download, I set the download state to the “downloaded” flag. The problem with this is if the user navigates away from the the current tableview data and to another, the competition block will set the wrong NSManagedObject – it does the look-up based on the NSIndex.

In a nutshell I would like to know how I can pass a object along with my AFHTTPRequestOperation so that on completion an operation can be performed on it. It could be as simple as an int or string and I can do an NSPredicate request based on this value.

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        [self.operations addObject:operation];
        [self.inProgressDownloads addObject:indexPath];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *extension = [[self.selectedFile.name componentsSeparatedByString:@"."] lastObject];
        NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat: @"%@.%@", self.selectedFile.item_id, extension]];
        operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
        self.selectedFile.status = DOWNLOADING;
        [self.tableView beginUpdates];
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView endUpdates];

        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"File downloaded to: %@", path);
            self.selectedFile.status = DOWNLOADED;
            self.selectedFile.is_stored = @YES;
            [self.tableView beginUpdates];
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
            [self.tableView endUpdates];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
            self.selectedFile.status = DOWNLOAD_ERROR;
        }];

        [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
            dispatch_async(dispatch_get_main_queue(), ^{
                float percent = (float)totalBytesRead / (float)totalBytesExpectedToRead;
                NSLog(@"totalBytesExpectedToRead %d", (int)(percent * 100));
            });
        }];

        [operation start];
  • 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-17T08:22:08+00:00Added an answer on June 17, 2026 at 8:22 am

    In a nutshell I would like to know how I can pass a object along with my AFHTTPRequestOperation so that on completion an operation can be performed on it. It could be as simple as an int or string and I can do an NSPredicate request based on this value.

    This is where the power of closures/blocks comes into play. You do not need to pass anything special into a block. You can refer to any object which is lexically visible in the block and it will be available when the block is run.

    From Apple docs (specifically, point 2):

    Blocks represent typically small, self-contained pieces of code. As such, they’re particularly useful as a means of encapsulating units of work that may be executed concurrently, or over items in a collection, or as a callback when another operation has finished.

    Blocks are a useful alternative to traditional callback functions for two main reasons:

    1. They allow you to write code at the point of invocation that is executed later in the context of the method implementation.
      Blocks are thus often parameters of framework methods.

    2. They allow access to local variables.
      Rather than using callbacks requiring a data structure that embodies all the contextual information you need to perform an operation, you simply access local variables directly.

    If I understand correctly what you are trying to do, this should work:

    TYPE_HERE* localSelectedFile = self.selectedFile;
    
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"File downloaded to: %@", path);
            localSelectedFile.status = DOWNLOADED;
            localSelectedFile.is_stored = @YES;
            [self.tableView beginUpdates];
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
            [self.tableView endUpdates];
           ...
    

    where I am storing the value of self.selectedFile into a local variable at the moment operation is executed; then I am using that local variable into the block body. This is legal (this is the power of closures I was referring to: they keep with themselves information about their execution context) and will give you the reference to the object you have been processing.

    You might find this interesting for an overall view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Current Process: I have a tar.gz file. (Actually, I have about 2000 of them,
Basically I have a Login window which should close once the user logs in
In my current design, I have HiLo setup to have a MaxLo of 1000
I have many competing update statements in a multi-application environment. With the current design,
My App requires Daily reports based on various user activities. My current design does
This is my current table design I have a many to many relationship. For
i have the current design in mysql : Table filesubject Is there a way
I have a certain POJO which needs to be persisted on a database, current
In my current framework's design, I peruse all types of specific assemblies and do
The design of the current app I'm working on calls for a WCF Service,

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.