简体   繁体   中英

iOS app crash recover method

Since 2 yrs I have been trying different ways to find the solution of app crash while click back button.

My application scenario:

In a tableview contoller I have to load list of users, On view did load I call getData(Asyncronous download) API method to load data. At the time of data download, If user press back button my application gets crash due to null value objects. That says all of my variable memory deallocated.

To overcome this problem, I used some loading indicator which lock UIScreen untill data download.

Questions:

  1. Is there any alternatives to prevent crash, UIScreen Lock
  2. Other applications use Activity Indicator in Menu bar without UIScreen Lock. How they are doing?

Need help to recover this issue

Here is my sample code to download data : Below code doesnt crash app. But it download data even I cancel operations on dealloc viewDidLoad:

ShowNetworkActivityIndicator();
_processQueue = [[NSOperationQueue alloc] init];
_processQueue.maxConcurrentOperationCount = 4;
_processQueue.name = @"Events Processing";
[self loadData];

loadData:

-(void)loadData { [_processQueue addOperationWithBlock: ^ {

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[[NSURL alloc] initWithString:@"https://restcountries.eu/rest/v1/all"]]; 
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];



    NSDictionary *search = [NSJSONSerialization JSONObjectWithData:[data dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];

    [[NSOperationQueue mainQueue] addOperationWithBlock: ^ {
        _countryListArray=[search mutableCopy];
        [self.tableViewSample reloadData];
        HideNetworkActivityIndicator();
    }];

}];

}

I tried cancelAllOperations in dealloc:

[_processQueue setSuspended:YES];
[_processQueue cancelAllOperations];

Can you try inserting the reload data is dispatch_async(dispatch_get_main(),void (^){}); callback , main thread , I think the reload happening in the background thread is crashing the app.

[[NSOperationQueue mainQueue] addOperationWithBlock: ^ { _countryListArray=[search mutableCopy]; dispatch_async(dispatch_get_main_queue(), ^{ [self.tableViewSample reloadData]; HideNetworkActivityIndicator(); });}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM