How to create many single-serving ASIHTTPRequest class deallocated after end of request?
I display an UIImage in a view.开发者_运维问答 Sometimes the requested image isn't in the main bundle or in user folders. If this is the case, I want to :
- start downloading the image from a remote server
- set a UIActivityIndicator on the target UIImageView
- after complete download, save the downloaded image in a folder
- load the UIImage in the UIImageView and stop the UIActivityIndicator
Dealing with the case where the user change the view while the download isn't finished is hard. I do want the download to continue until the end. On the other end, he can go to another view lauching a parallel download.
I have created a DownloadManager with a static method +getImage:(NSString*)URL targetUIImageView:(UIImageView*)targetUIV I want this method to instanciate new ImageDLClass with each it's own ASIHTTPRequest, it's own target (assign) UImageView. Testing if this target == nil permits to know if view is deallocated or not.
What I can't figure out is, who will retain all the ImageDLClass instances, and how could I deallocate them after download finished.
I look for architecture ideas, I already know how to code each point. Do you have architecture ideas ?
Thanks for your help !
kheraud
I finally use a better thing.
The magick keyword here is "userInfo". In fact you can link anything with a request and retreive this "anything" in the delegate methods.
My architecture is the following :
- A singleton ImageManager
- ImageManager has one attribute : an array containing the request not finished yet
- ImageManager has a method which launches an ASIHTTPRequest and add this request to its array
- In the userInfo of each request there is a pointer to the target UIImageView
- UIActivityIndicatorView is created (with 555 tag number) and added as subview to the target UIImageView after having started the asynchronous request
- UIActivityIndicatorView is removed using its tag number
The array attribute permits to cancel and release all non-finished requests if the ImageManager is deallocated.
Hope it can help !
kheraud
精彩评论