0

I have used [NSURLSession sharedSession] to get data from server.While getting the data if the app moves to background and after sometime if it comes to foreground I get "Connection was lost" error. So I implemented getting data in background and used beginBackgroundTaskWithExpirationHandler. But the problem is after sometime the app restarts from the beginning since I ended background task which I don't want.Is there a way I fetch data when data is loading and app moves to background and on opening the app it should show the page it was prevoisly displaying before moving to background.

[self beginBackgroundUpdateTask];
    
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
^(NSData * _Nullable responseData,
  NSURLResponse * _Nullable urlResponse,
  NSError * _Nullable error) {
     if (error) {
     NSLog(@"dataTaskWithRequest error: %@", error);
}

else{
      NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)urlResponse;
        if([httpResponse statusCode ]>=200 && [httpResponse statusCode]<300)
         {
             //Do UI related things
         }
         else
         {
          //Display error here
         }
[self endBackgroundUpdateTask];
    }]resume];


- (void) beginBackgroundUpdateTask
{
 backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [self endBackgroundUpdateTask];
    }];
}

- (void) endBackgroundUpdateTask
{
    [[UIApplication sharedApplication] endBackgroundTask: backgroundUpdateTask];
 backgroundUpdateTask = UIBackgroundTaskInvalid;
}
Honey
  • 2,840
  • 11
  • 37
  • 71
  • Not sure that I got you final question - is it about issues with background task or with the app state restoration? Take a look on [Preserving Your App's UI Across Launches](https://developer.apple.com/documentation/uikit/view_controllers/preserving_your_app_s_ui_across_launches) documentation – ATV Nov 27 '20 at 16:09
  • @ATV It is about fetching app data in background.While fetching data if app moves to background I get connection lost errors because of asynchrounous request dont fetch data in background . For that I have used beginBackgroundUpdateTask. But using this kills my app & doesnt restore the state of the viewcontroller which was displayed before movin to background. Hence app always starts from the beginning due to beginBackgroundUpdateTask. How to show the viewcontroller which was displayed prevosly – Honey Nov 30 '20 at 09:24

1 Answers1

0

Instead of using the shared session, create your own NSURLSession background session, and create a download task in that. That way, when the user backgrounds your app, the request will keep running.

When the request completes:

  • If you're in the foreground, read it immediately and handle it.

  • If the download completes while your app is in the background, move it to a location in your app container directory, then load it after the user foregrounds the app.

  • If your app gets relaunched in the background because the download completed after your app got terminated, move the file aside for later use, and then reload it after the user relaunches the app.

dgatwood
  • 10,129
  • 1
  • 28
  • 49