Background Upload - iphone

I am looking for a direction on a background image upload on the iPhone, background meaning in my app while other interface items are processing and views are changing.
I was thinking that there would be a way in the appDelegate to spawn a process to upload the file, then have a delegate return the status.
Has anyone seen an example of this?

I plan on using ASIHTTPRequest in my app to handle communications to web servers in the background.

Related

How do you cancel all pending and current image download requests in AlamofireImage?

Introduction:
Hello, I'm currently using the AlamofireImage framework to download images in my app. I'm primarily using their ImageDownloader to download images and am using a singleton instance of this ImageDownloader to download images in a custom URLImageView I built in SwiftUI.
As far as I can tell, there doesn't seem to be a cancelAll method on the ImageDownloader class. The best there is, is a cancelRequest(with requestReceipt: RequestReceipt) method, but this requires keeping track of RequestsReceipts which is overhead I'd prefer not to manage. I've played around with this as well but it doesn't seem to cancel currently downloading images, only pending in the queue (from what I've found in testing)
Problem I'm trying to solve:
I'm running into an issue in my app where I have a few screens back to back that are VERY image heavy and display many images. When a user is on a slow cell service connection (not wifi), I see that theres a bottleneck that begins to happen where when a user goes to the next screen, it can't begin downloading the new images because its bogged down waiting for the previous screens images to complete downloading.
I'd love for a way to have a generic cancelAll method I can call that just cancels everything already pending/downloading since as I don't need previous screens to continue downloading - I need the new screen to have all the bandwidth priority to download new images.
Solutions I've Tried:
The closest I've come to replicate the behavior I want is with the following method:
func cancelAll() {
// invalidate and cancel session
alamofireImageDownloader.sessionManager.session.invalidateAndCancel()
// re-init alamofire image downloader
alamofireImageDownloader = ImageDownloader(
configuration: ImageDownloader.defaultURLSessionConfiguration(),
downloadPrioritization: .fifo,
maximumActiveDownloads: 4,
imageCache: cache
)
}
This solution makes me nervous however because it feels weird to have to re-init the ImageDownloader every time I go to a new screen in order to cancel all pending and current image downloads.
That said this solution does work, and it allows new screens to get the full bandwidth to download images and is no longer blocked by previous screen images.
Would love some thoughts on best practice on how to do this or comments on whether my current solution is safe/smart to do.

ios load images from web server and show on UITableViewCell

For the iPhone 5.0 (ARC) I am getting lot of image(image url's) and text data from server in a json form. My requirement is to load the first five images from the server and when the user scrolls down the next five images load and so on. I need to show it in a UITableViewCell. By doing this I can reduce the network calls and make the application faster on the device.
Currently, I am using a background thread to load images, but they continuously load in the background. I don't want to do it like this.
I strongly encourage you to use the AFNetworking framework (that is really great and complete for all network-related tasks).
It comes with a category on UIImageView that allows you to set the UIImageView's image directly from an NSURL, managing everything for you in the background, like downloading the image or fetching it from its cache, cancelling the request if you change the URL later to avoid useless requests, etc.
Then in your tableView:cellForRowAtIndexPath: you can simply write this kind of code:
[cell.imageView setImageWithURL:yourURL];
And you're done. It will work even when the recycling mechanism of the UITableView is in action when you scroll, which would be quite a pain to manage using other methods.
Take a look at SDWebImage. It's a really neat library to download images asynchronously and also caches them.

load and cache remote image in background thread

I am working on an iPhone app.In this app I need to display many images in uitableview.I am able to load the image in the background thread.But can anyone suggest how can I cache the downloaded image in background thread.I am able to cache the image but it is happenening in foreground not in background.Thats why the UI gets blocked until the caching completes.Thanks in advance.
Hi you can try SDWebImage Cache, it is very easy to use and it will do caching part.
The caching interval is set to one week, you can check it in SDImageCache.m.

iPhone download / parse xml file on app launch?

I'm working on a storyboard app in which one of it's tabbed views is a mapView that downloads points from an xml file on a server. Everytime the app is launched and that tab in the tab bar controller is selected, it takes a second to download the file and parse it. Worse still, if there is no access to internet, the app crashes :/
I would like to try to use my getDataService (which downloads and parses) during the splash screen of the app instead of on ViewDidLoad of the viewcontroller (is this in appdelegate under didFinishLaunchingWithOptions?) and let it do its thing then, or in the background, and keep working in the background if the app is closed.
Then I'd like the array to be retained for future use (does this have to do with dictionaries?) and only re-download and parse the xml when the app is fully restarted.
How can I do these things? If anyone has examples or links to tutorials and examples that do these things, I'd be very grateful. I've been searching for a while, but I don't know what to search for.
Actually DO NOT do this during didFinishLaunchingWithOptions:
The reason is that iOS will kill any application that takes too long to load. I don't have the relevant documentation to hand, but iOS expects your app to finish launching within a specific period of time (I believe it's around 3 seconds or so) and if this method is not finished within that time frame you app is deemed to have hung and iOS will kill it.
The recommended technique is that if you have long running code is to start a background thread with the code on it.
The whole idea is to get the user to a usable interface as quickly as possible. Note that the debugger disables iOS's kill function, it's only active when your code is on a device and no you cannot disable it programmatically. So your code will appear to be fine when developing but fail when you run it for real.
If you need to display something whilst loading, I'd recommend putting the long running code on a background thread and continuing on to a temporary view which is basically a copy of the splash screen. then when you data is available, load up your interface.
If you do this from the AppDelegate applicationDidFinishLoading function, and assign it to a property that you define for you AppDelegate, your loading screen will still be visible while your data is being downloaded and parsed.
- (BOOL)application:(UIApplicatioN *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"loading!");
sleep(5); //delay to show you that the log happens before the loading screen goes away
return YES;
}
Make the first screen that shows when the app launches (after the Default.png splash screen) be the same or a very similar image, but give the user feedback that something is happening -- e.g. a UIActivityIndicator.
If your app doesn't fail gracefully when launched with no internet connection, Apple will reject it. Show a relevant message in this situation which advises the user something like either of these:
1) No internet connection, so functionality of this app may be limited
2) No internet connection, and this app will not function with it

Iphone memory warning pause application

I am writing an application for a 2g device and I am receiving memory errors. The errors are created by saving a large file to the device while searching sql libraries and displaying popups (picker view mostly).
I know this is a bit vague but I was wondering if there was a way to pause user interaction while letting the device complete processing it's data. I have tried using sleep(), and [NSThread sleepForTimeInterval: 0.5], but this all pauses the application also.
Preferably this would be called in didReceiveMemoryWarning to let the app catch up processing data while blocking user interaction.
Thanks
One option is to add an invisible (i.e. transparent) view on top of all the others. Ideally, you reduce its alpha value to make it sort of gray out the underlying user interface and display a spinner or something like that to indicate that the device is working.