-
Notifications
You must be signed in to change notification settings - Fork 10.5k
New Progress Reporting API using NSProgress
#3187
Conversation
progress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress | ||
success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success | ||
failure:(nullable void (^)(NSURLSessionDataTask * __nullable task, NSError *error))failure; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the sibling method be deprecated?
Also note this does not implicitly add progress to the current progress. The user would need to do this explicitly. |
Discussed this issue in-depth on Slack with @kcharwood and I think this is the correct approach. Passing the |
And lines up with the queue that Alamofire passes back the progress block. |
I went ahead and added a deprecated flag to the older methods in the API, and will release beta 3 with them there. We can then decide to pull them for the final release, or leave them in to remove for a future release. @0xced @jshier @kylef I'd like to get this merged in first thing in the morning, and a new release cut tomorrow. Let me know if you have any other feedback! |
NSProgress
Once I merge this in today, I'd still like to get some more feedback from the community as we wrap up the beta period. All thoughts are welcome! |
These changes expose a simpler, block based progress reporting API, while maintaining the ability for more advanced features using KVO on NSProgress.
0e98de4
to
423d83e
Compare
Current coverage is
|
New Progress Reporting API using `NSProgress`
@kcharwood , In AFURLSessionManager KVO For Progress ,the invoke sequence is different in iOS 8.x and in iOS9.x is Like this: as you can see in iOS 8 i will get fractionCompleted 1.000000 , that's incorrect, cause this KVO invoke before countOfBytesExpectedToSend invoke. so countOfBytesExpectedToSend is equal to 0. |
This issue is the documentation for v3.0 API changes per https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-3.0-Migration-Guide Just as a note to future readers, in AFN 2.0, your PUT request progress could be tracked in a block like this:
However, in AFN 3.0, the only option available is to use KVO on the NSProgress like this:
and then you need to set up observers. Perhaps this was not intentional, and PUT may get more love in the future. |
@fulldecent , you can read AF3.0 code ,in AF, it has use KVO to nofity progress changes. |
The impetus for this change was a bug related to: https://github.com/AFNetworking/AFNetworking/issues/3380 Which points to an issue where AFURLSessionManager could get in a state where it does not inform the AFURLSessionManagerTaskDelegate to un-observe cancelled (or otherwise completed) tasks before they are deallocated. While the above issue is specific to background tasks/sessions, we have observed it on sessions with default configurations as well. Instead of debugging the KVO issues, I decided to replace the KVO code with in favor of the appropriate NSURLSession delegate methods. Running all unit tests shows that this approach appears to be working. The pull request that made this change (AFNetworking#3187) mentions that its goal was to remove the need for AFNetworking API consumers to use a KVO API for getting progress. This makes sense, but I was unable to see any reason why KVO was used internally for getting progress information rather than the NSURLSession Delegate methods. Because of this, It isn't clear to me what the historical reasons were/are for KVO being needed to satisfy the progress requirements of the AFNetworking API. With any luck, KVO is no longer needed and we can eliminate this class of crashes by eliminating KVO.
These changes expose a simpler, block based progress reporting API, while maintaining the ability for more advanced features using KVO on NSProgress.
Issues #3158 and #3163 got me thinking that progress was easier to use with old NSURLConnection based API's, and we shouldn't force anyone to use KVO for progress updating, so I decided to take a stab at implementing a block based approach, while also streamlining the accessors for individual upload and download progress.
Note that
NSURLSessionTask
sees progress as two distinct items: upload progress and download progress, as reported bycountOfBytesExpectedToReceive
,countOfBytesReceived
,countOfBytesExpectedToSend
, andcountOfBytesSent
. Therefore, I've decided to track these two things independently with two distinct progress objects, managed by the internalAFURLSessionManagerTaskDelegate
.API Changes
AFHTTPSessionManager
exposing download progress block for GET, and upload progress block for POST.AFURLSessionManager
allowing upload and/or download NSProgress objects to be retrieved for any task.AFURLSessionManager
exposing download and upload progress blocks for data task, download progress block for download tasks, and upload progress block for upload tasks.Open Questions
AFHTTPSessionManager
be deprecated and/or removed? For example, there are now four POST methods in that API, rather than two as before.@0xced @cnoon @jshier @kylef would love your eyes on this. Probably the biggest code change so far in the 3.0.0 branch. I think I got all the KVO management setup internally correctly, but would appreicate a review.