如何在AFNetworking 2.0中使用Progress参数 [英] How to use Progress parameter in AFNetworking 2.0
问题描述
我正在尝试将AFNetworking 2.0与NSURLSession一起使用。我正在使用方法
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;
我应该如何使用progress参数。该方法是一种非阻塞方法。因此,我将不得不听取' progress
'来获取更新。但参数不会占用属性。仅采用局部变量(NSProgress * __ autoreleasing *)。我无法将KVO添加到本地var。
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.
我不确定如何使用。
推荐答案
任何时间参数以 **
给出,这意味着您应该将指针传递给指向现有对象的指针,而不是指向实际对象的指针通常会这样做。
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.
在这种情况下,你传入指向 NSProgress
对象的指针,然后观察该对象的变化以获得更新。
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.
示例:
// 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];
然后报告:
- (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);
}
}
这篇关于如何在AFNetworking 2.0中使用Progress参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!