简体   繁体   English

iOS - 如何在AFHTTPClient完成所有操作时收到通知

[英]iOS - How to get notified when AFHTTPClient is done with all operations

I use AFHTTPClient to upload files to an API as a AFMultipartFormData . 我使用AFHTTPClient将文件作为AFMultipartFormData上传到API。 AFHTTPRequestOperation -s are created in a loop so I don't know how many they are or which one will be the last operation. AFHTTPRequestOperation -s是在循环中创建的,所以我不知道它们有多少或哪一个是最后一个操作。

How can I get notified when AFHTTPClient is done with all operations? AFHTTPClient完成所有操作后,如何收到通知? there is operationQueue property which can count operations for me, but I don't want to add an observer for that.. operationQueue属性,可以为我计算操作,但我不想为此添加一个观察者..

您可以使用GCD在后台放置一个代码块,在队列中调用waitUntilAllOperationsAreFinished ,然后发出通知。

If @patric-schenke's answer doesn't work (for example, if you want to count only upload operations and not all operations, you can create a counter. Create an NSInteger , and increment it when you add an operation. Make sure to decrement it in both the success and failure blocks. 如果@ patric-schenke的答案不起作用(例如,如果你只想计算上传操作而不是所有操作,你可以创建一个计数器。创建一个NSInteger ,并在添加操作时递增它。确保减少它在成功和失败的块中。

Why don't you want to add an observer to the operationQueue , though? 你为什么不想把一个观察者添加到operationQueue呢? I think that'd get what you want with the least amount of code. 我认为用最少量的代码得到你想要的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM