Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
639 views
in Technique[技术] by (71.8m points)

ios - How to use Progress parameter in AFNetworking 2.0

I am trying to use AFNetworking 2.0 with NSURLSession. I am using the method

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
                                         fromFile:(NSURL *)fileURL
                                         progress:(NSProgress * __autoreleasing *)progress
                                completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler;

How am I supposed to use the progress parameter. The method is a non blocking method. Hence I will have to listen to the 'progress' to get the updates. But the parameter wouldn't take a property. Only takes a local variable(NSProgress * __autoreleasing *). I can't add KVO to a local var.

I am not really sure how to use.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Any time an argument is given as ** it means that you're supposed to pass in the pointer to the pointer to an existing object, not a pointer to the actual object as you would normally do.

In this case, you pass in a pointer to a pointer to an NSProgress object and then observe the changes in that object in order to get the updates.

Example:

// Create a progress object and pass it in
NSProgress *progress;
[sessionManager uploadTaskWithRequest:request fromFile:fileURL progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    // Completion code
}];

// Observe fractionCompleted using KVO
[progress addObserver:self
          forKeyPath:@"fractionCompleted"
             options:NSKeyValueObservingOptionNew
             context:NULL];

Then it gets reported in:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

    if ([keyPath isEqualToString:@"fractionCompleted"] && [object isKindOfClass:[NSProgress class]]) {
        NSProgress *progress = (NSProgress *)object;
        NSLog(@"Progress is %f", progress.fractionCompleted);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...