1

I am developing an iPhone app in which, I want start background thread for downloading image from server. while downloading the image if user wants to cancel a download then on click of button he can cancel downloading means I want to stop execution of background thread.

is it possible to do ? in iPhone

hochl
  • 12,524
  • 10
  • 53
  • 87
Krunal
  • 6,440
  • 21
  • 91
  • 155

3 Answers3

0

you can start a continue the background download with this code.

taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
    }];

//code to download .

[[UIApplication sharedApplication] endBackgroundTask:taskId];

you can call

[[UIApplication sharedApplication] endBackgroundTask:taskId];

to stop the background task.

But this download will continue only for few minutes . Then app will crash if it continue downloading.

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Bala
  • 510
  • 2
  • 9
  • it is background task identifier declare as UIBackgroundTaskIdentifier taskId; – Bala Feb 20 '14 at 12:42
  • I wrote this my method is not getting fired `taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) { [self downloadImage_3:indexPath]; }];` – Krunal Feb 20 '14 at 12:47
  • you have added [self downloadImage_3:indexPath]; in wrong place. add it where i have mentioned //code to download – Bala Feb 20 '14 at 12:52
  • `taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) { }]; [self downloadImage_3:indexPath];` wrote this but it dowwnloads image on main thread.. – Krunal Feb 20 '14 at 13:12
0

If you want to perform an operation in the background, then you'll need to wrap it in a background task. It's also very important that you call endBackgroundTask when you're finished - otherwise the app will be killed after its allotted time has expired.

Something like this should work for you:

Declare a property:

@property(assign, nonatomic) UIBackgroundTaskIdentifier backgroundFetchTask;

Then:

- (void) fetchImageInBackground
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    [self beginBackgroundFetchTask];

    NSURLResponse * response = nil;
    NSError  * error = nil;
    NSData * responseData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];

        // Store your image

        [self endBackgroundFetchTask];
    });
}

- (void) beginBackgroundFetchTask
{
    self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [self endBackgroundFetchTask];
    }];
}

- (void) endBackgroundFetchTask
{
    [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
    self.backgroundFetchTask = UIBackgroundTaskInvalid;
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • I wrote this code in `dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self beginBackgroundFetchTask]; [self downloadImage_3:indexPath]; [self endBackgroundFetchTask]; });` in my `cellForRowAtIndexPath` but and i am cancelling background thread in `didSelectRowAtIndexPath` but it crashes my app – Krunal Feb 20 '14 at 13:31
  • What it is you want to cancel? The image download? If so, you'll need the change the download method, but the background task is still the same. Don't forget to call `endBackgroundFetchTask` as well – Ashley Mills Feb 20 '14 at 13:32
  • yes i want to cancel downloading of image, i am calling `[self endBackgroundFetchTask];` from `didSelectRowAtIndexPath` – Krunal Feb 20 '14 at 13:33
0

You can use AFNetworking for this purpose. https://github.com/AFNetworking/AFNetworking

AFImageRequestOperation exactly does this.

Example to download image:

AFImageRequestOperation * _operation = [AFImageRequestOperation imageRequestOperationWithRequest:request success:^(UIImage *image) {
    // image is your image
    _operation = nil;
}];

[_operation start];

Example to cancel thread:

[_operation cancel];

So in your case -> declare variable AFImageRequestOperation *_operation; in the @interface or @implementation and use it to download your images in background.

I would do a singlton class MyImageBackgrounfDownloader

Alex Wilson
  • 2,421
  • 11
  • 13