I can able to download a file when an URL is given using the delegate methods of NSURLConnection. When a download is active, its download progress like percentage downloaded, transfer rate, bytes received are shown in the UITableView. So, when I initiate a download, the first cell of the table is active till the download completes. Everything is perfect for a single download.
My problem is, I have no idea how to handle multiple asynchronous downloads. If the URL is given when a download is active, the second cell of the table should be activated and the progress should be shown. I surfed many blogs and forums regarding this. Many suggested to use ASIHTTPRequest. But I am trying to handle the NSURLConnections in an array and hit the requests. When I tried, the first active download is overridden by the second request.
Please help me with some ideas to deal this problem using array, without ASIHTTPRequest. Thank you in advance.
Assuming that you're making multiple NSURLConnection objects, one per request just store the connection that each table view cell is referring to in an array.
Then, when the delegate methods are called, they should all pass back the connection object that they are referring to i.e. connection:didReceiveData:'s connection parameter.
You can use this parameter to work out which table cell this delegate call is referring to.
Related
I am downloading Data with RestKit by creating RKObjectRequestOperations and adding them to RestKit's queue:
RKObjectRequestOperation *operation = [RK objectRequestOperationWithRequest:request
success:...
failure:...];
[RK enqueueObjectRequestOperation:operation];
That one works well. Additionally, this data is displayed in a list view that contains UIImageViews displaying related user icons. These user icons, however, are not downloaded via RestKit, but via the underlying AFNetworking library. UIImageView+AFNetworking does the job also pretty well (including the caching):
[self setImageWithURLRequest:userIconRequest
placeholderImage:anonymousUser
success:nil
failure:...];
The problem is that those 15 user icon requests from the image views block the processing of the RestKit request that should be loading the next page immediately. I can see the list displaying the "loading" row as well as the first user icons. In the moment the last image finished loading the next page appends itself.
Looking into the implementation of UIImageView+AFNetworking shows that it is using an own NSOperation queue instance that is serializing requests. However that should not interfere with RestKit I suppose.
Also adding NSOperationQueuePrioritys to all requests does not change a thing. Maybe internally, network requests are serialized in a different way? How can I prioritize those requests?
Thanks in advance.
NSURLConnection has an undocumented maximum number of connections.
Additionally, UIImageView+AFNetworking's operation queue has NSOperationQueueDefaultMaxConcurrentOperationCount maximum current requests, which is likely a bad choice for your use case according to this convincing-sounding AFNetworking discussion.
You need to throttle. I see two simple solutions:
Modify UIImageView+AFNetworking to have, say, 4 maximum concurrent operations.
Modify UIImageView+AFNetworking to use the same operation queue as RestKit, in which case the priority you set will matter.
Sorry to bother with yet another NSURLConnection question, adding to the over one thousand already here.
The scenario is as follows. In an iPhone app using dynamically loaded augmented reality features, the user is prompted to download new AR models as these are made available. The models can be several MB large, so the user should be given an indication of the total size of all models to be downloaded before deciding to do so.
To find out how large each file is I want to use an asynchronous NSURLConnection but then to stop the download once I have got the response ([NSURLResponse expectedContentLength]). I can do this in the delegate's connection:didReceiveResponse: method.
My question is, how can I wait until this condition arises? How can I setup the NSURLConnection, let it start asynchronously and then wait until the connection:didReceiveResponse: method is called? I have tried using a NSCondition, letting this wait after setting up the NSURLConnection and in the connection:didReceiveResponse: method signalling the condition. But all this did was to freeze the main thread. Any ideas?
Maybe you could send a HEAD request instead of GET. This may depend on your server set up, but that should get you just the headers, including Content-Length. You ought to be able to use a NSMutableURLRequest so you can change the request method, and then read expectedContentLength on the response as usual.
Hi
i have plan to develop app similar to
http://itunes.apple.com/app/download-meter-for-wi-fi-3g/id327227530?mt=8
Please suggest me, which api's i have to look into?
Any tutorials avilable?
Thanks in advace.
You want to be looking into the UIProgressBar object, and the related delegate methods. When you open a NSURLConnection, you can retrieve the expected size of the thing you are downloading (this method will vary depending on where your data is coming from).
The NSURLConnection will download the object in chunks, calling the didReciveData method every time it receives a packet of bytes. Here you can calculate the percentage of the file downloaded (it will be decimal format, ie. 0.74/1.0) as you append the data and update the progress bar accordingly.
Check out the tutorial HERE
I've not found a answer to this question anywhere, but this seems like a typical problem:
I would like to send some POST-Requests (with ASIHTTPRequest, what I already do), but if something goes wrong, ther user can decide to "Try Later", that means, the task should be put on a queue and this queue should be read next time the application starts. So, that's my question: how to "save" the queue, so that the app can read it next time it starts? Is it possible to "read" the queue and try sending this POST-Request again, let's say, 10 min later, even if the application is not running?
What kind of documentation should I read in order to be able to do this?
I would be very glad to hear any answers. Thanks in advance.
P.S.: Another Idea I have: as I just have to Upload Photos, I could have a folder with all the Photos that still need to be uploaded, and when the App starts, the app looks at this folder and try to send all the photos in this folder. Does it make sense?
My approach for this issue would be like this:
Whenever you fail to send details - write content of the array to a file using '[NSArray writeToFile:]' you can use serialization if array contain any data which is custom defined (if your array contain standard cocoa objects(NSString,NSData etc) they already implemented with serialization )
When app launches; load the content from file directly to an array object ('[NSArray arrayWithContentsOfFile:]')
then construct http request and try sending. In application the data(in your case array) is stored/serialized not the request, you need to reconstruct the http request when you want to try one more time.(don't try serializing ASIHTTPRequest, you have reconstruct it)
I'm going to assume you've already looked at NSOperationQueue and NSOperation. AFAIK there is no built-in support for serializing NSOperation, but you could very easily write your own serialization mechanism for an NSOperation subclass that you use for posting data and write the an NSOperationQueue's operations to disk if something goes wrong.
Without knowing too many details it's hard to give a precise answer. There are many ways to write data to disk and load it again later, the direction you take will be largely dependent on your situation.
Some Background
I have an iPhone app with three UIWebViews, each used to show a different type of page. (The content is specifically designed for this purpose, they're not real web pages.) There are links associated with each specific web view. For example, a link tapped in WV1 might need to load in WV2 because it's a WV2-style link. (The webviews are in a paged UIScrollView, which I use to scroll to the currently loading webview. I can tell which URL loads where based on it's path.)
It makes sense to me to have a single UIWebView delegate that responds to all URLRequests (via webView:shouldStartLoadWithRequest:navigationType) and somehow decides which should load where.
The HTML I want to load in the webview isn't fetched directly from a page. I do something like [NSDictionary dictionaryWithContentsOfURL:] to get the object, part of which is the actual HTML. (Additionally, this might come from the network or a local cache.)
The Question
I need to cancel a request in one UIWebView and then load some arbitrary data into another. How should I be doing the subsequent load, so that it bypasses my interception?
Should I stop all intercept all NSURLRequests via the webview delegate method, then send a new NSURLRequest, that will actually get loaded, to the correct webview? I was thinking I'd subclass NSURLRequest so I'd be able to tell original requests separate from my doctored requests. But again, I don't want to make straight NSURLRequests, I want to fetch an NSDictionary and use one of it's values as the HTML.
I saw this article on filtering what loads in a UIWebView, but I don't think that's exactly what I want.
I'm trying to work my way through the URL Loading System Overview, but there's a lot there.
Ended up using custom URL schemes to identify the requests that I wanted to load vs. the ones I wanted handled.