IOS Iphone app randomly hangs indefinitely - ios

App being built on Xcode 4, for a base IOS level of 4 and above, tested on a variety of IPhones. Info in app is loaded from a JSON feed.
In my attempts to track down what's happening I've repeatedly debugged through this app, a fairly basic one that has a few views, overall nothing amazing or new in it as far as apps go.
The problem manifests itself occasionally, a lot of the time in our testing it has been if we've turned off Wifi to test on a cellular connection the app will hang when loading.
What happens is that the splash screen appears and the white spinning activity wheel spins as if it's loading the ad (an ad is displayed as part of the overall loading process) but it just sits there spinning.
So, most obvious question, is the ad the problem? Is it large or loading slowly? No, the JSOn feed is speedy (we check every time the app hangs and inbetween times as well to make sure it's all loading ok) and the ad is about 20k.
Debugging gets as far as the didFinishLaunchingWithOptions method which I'll list below, it reaches the end of that method and the debugger essentially reports nothing else. It just sits there, nothing visible in the XCode debug navigator, no break points hit.
If step through that method, once it gets to the end it goes about 6-7 steps into compiled code then simply does the same thing.
Here's the code from that method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
[self loadData];
splashViewController = [[OurSplashViewController alloc] initWithNibName:nil bundle:nil];
splashViewController.delegate = self;
OurWebViewController *webViewController = (OurWebViewController *)[[[tabBarController viewControllers] objectAtIndex:2] topViewController];
webViewController.url = [NSURL URLWithString:#"http://m.testURL.com/this_app/"];
webViewController.path = #"/MyPath/Sample";
[window addSubview:splashViewController.view];
[window makeKeyAndVisible];
return YES;
}
So, it gets to the 'YES' of that method and then the debugger sails off into the land of ones and zeroes, reporting nothing back and hanging the app.
Has anyone got any idea what could be causing this?
UPDATE:
Seem to have isolated that the hanging comes down to a JSON call that is sent but no response is received. So my code should handle that after a while.
Here's the JSON code, maybe someone can notice something I'm missing in there:
- (void)fetchWithURL:(NSURL *)URL {
if (!URL) {
return;
}
self.jsonURL = URL;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:self.jsonURL];
if (timeout) {
[request setTimeoutInterval:timeout];
}
[request addValue:#"gzip" forHTTPHeaderField:#"Accept-Encoding"];
connection_ = [[NSURLConnection alloc] initWithRequest:request
delegate:self
startImmediately:YES];
[request release];
if (connection_ != nil) {
if ([self.delegate respondsToSelector:#selector(JSONFetchWillBeginDownload:)]) {
[self.delegate JSONFetchWillBeginDownload:self];
}
receivedData_ = [[NSMutableData alloc] initWithCapacity:0];
} else {
if ([self.delegate respondsToSelector:#selector(JSONFetch:downloadDidFailWithError:)]) {
[self.delegate JSONFetch:self downloadDidFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorUnknown userInfo:nil]];
}
}
}
According to Charles the reason for the failed response was:
Client closed connection before receiving entire response
I wonder is my timeout period too short?

If the spinning wheel (which is presumably part of the splash screen) is appearing, then the method you've posted is obviously working. If the splash screen is not disappearing, the obvious place to start debugging is in the code that sends the request and the code that receives the response then dismisses the splash screen.
Use a proxy like Charles to verify that the request is going out and a response is coming back. If that is the case, debug the code that receives the response and dismisses the splash screen. If that isn't the case, debug the code that sends the request.
If you don't get anywhere, I suggest posting the relevant code.

Use Charles Proxy for debugging the data exchange with your remote services and the ad-server.
See this tutorial for setting everything up.

Looks like my timeout period was too short for the ads and as a result if the user was off WiFi then the JSON took a little too long to load, causing the issue.
Fixed it by upping the timeout to a normal level.

Related

Multiple POST Requests Occurring

I am seeing strange behaviour on an iOS app with a Rails/Heroku backend. I'll try to give as much detail as possible. Hopefully someone can point out a few possible areas where the problem could arise.
I wrote an iOS 7 app using XCode 5 where a user can log in and POST new articles and comments. I am using the AFNetworking library to communicate with my backend server (Rails 4, Heroku & Postgres). I am POSTing and GETing in the JSON format so I make use of AFJSONRequsetOperation to handle communication.
All my POST's use this method:
- (void) Post:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success
:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON))failure
// allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:#"www.google.com"];
reach.unreachableBlock = ^(Reachability*reach)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
[HUD flashMessage :#"Whoops" :#"No internet connection" :window];
});
failure(nil, nil, nil, nil);
};
reach.reachableBlock = ^(Reachability*reach)
{
// prepare base URL and calculate signature
// Something like: https://myapp.herokuapp.com
NSURL *url = [NSURL URLWithString:BasePath];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient setDefaultHeader:#"Accept" value:#"application/json"];
// Generates the POST URL
// Something like: https://myapp.herokuapp.com/api/v1/articles.json
NSString *basePath = [self GetPostURL];
NSLog(#"Post %# With parameters: %#", basePath, Parameters);
NSMutableURLRequest *request = [httpClient requestWithMethod:#"POST" path:basePath parameters:Parameters];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:success failure:failure];
[operation start];
};
[reach startNotifier];
}
I did a bit of testing on my iPhone 5. Viewing the list of articles in a tableview, viewing article details in a detail view and then another tableview to view and add comments. I did a lot of testing and noticed no problem. Eventually I saw that there was 3 of the exact same comments. I thought I must of double-tapped on the 'Add' button so disable the button on-tap. I thought no-more about it as it was near impossible to re-create.
I have since updated my phone to iOS 8 and updated my XCode and SDKs to reflect this.
I put the app on my friends phone (iOS 8 & iPhone 5 C). She used it for a day without seeing any problems. She viewed articles, comments and added her own with no problems.
Today was a different story.
Firstly, a bit of background information. When you open the app - I refresh the list of articles so the app usually appears showing the old list of articles, then a loading icon appears, the tableview flashes and the newest articles appear.
When she opened the app today - it flashed multiple times, it looked like 10 or so. When the tableview appeared, the article she posted yesterday appeared twice. The exact article (title, description ,lat/lng, image) appeared twice but with different timestamps.
I thought the app was playing up on her phone so I checked the backend. It had two separate articles, different ids and timestamps but an exact copy otherwise.
Looking at the logs, there was something like 20/30 GET requests from her account. This seems to correspond with the flashing upon opening. It was updating the tableview multiple times.
Then the logs showed a POST request with all the params she POSTed the day before. Its as if she actually posted the exact same article today - but obviously she didn't.
Has anyone seen this kind of thing before?
The only things I can think of are:
The add article view controller was not destroyed after the initial post and somehow was made active again. I presumed ARC would handle all this?
Perhaps the AFNetworking library doesn't get flushed and thinks it needs to post again? Albeit nearly 24 hours later.
Is there a request batch operation on the library? Maybe this could be a problem?
I use a 'dispatch_get_global_queue' when posting. Is this okay?
On successfully adding an article, I move to the list again using the below code. Maybe there is a problem with it that I am unaware of?
ArticleListViewController *articleList = (ArticleListViewController *)[self.storyboard instantiateInitialViewController];
articleList.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self articleList animated:YES completion:nil];
I am at a loss trying to even replicate it, not to mention fix it.
I assume I am correct in pointing the finger at the app? It doesn't seem to be a problem with heroku/rails.
Any help would be greatly appreciated.
Update
I just had this issue again.
I did some testing yesterday, adding 3 articles. Everything worked as expected. I would occasionally go into the app every few hours to make sure everything is okay. I just opened the app now and it started flashing/flickering. It updated the list with a new article I posted yesterday. I did nothing with and it flickered again, and the same article was added again (so thats 2 duplicates with todays created_at date). I continued doing nothing and in a few seconds the other 2 articles that I added yesterday had been updated. One article was added 3 times, one article 2 times and one article was just duplicated once. This is bizarre behaviour.
Could there be some sort of global variable or something that keeps all the data? Maybe the controllers aren't removed completely?
Brian
After endless testing. I found the problem.
The duplicate POST(s) occurred at random. When it did occur it seemed to only happen 1% of the time, sometimes 5 minutes later, sometimes a day later.
I eventually figured out that it had something to do with switching from 3G to WiFi.
Adding an article on 3G, then changing to WiFi would cause a duplicate.
The problem is with the Reachability blocks in my above code.
I start the reachability notifier and set up two blocks for reachable and unreachable.
These blocks get hit when the phone changes reachability state. So moving from 3G to Wifi hit the reachable block again, resulting in a duplicate POST.
A simple fix is to call the following line at the start of each block to prevent further updates:
[reach stopNotifier];
Or, implement reachability like:
-(BOOL)reachable {
Reachability *r = [Reachability reachabilityWithHostName:#"www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if(internetStatus == NotReachable) {
return NO;
}
return YES;
}
// In POST method
if([self reachable])
{
// Do POST
}else{
// No connection
}

Objective c Thread 1 signal SIGSTOP app crashes

I'm trying to track down an error that appears to be definitely a timing issue. I have an application that uses a Universal Framework. When the process is complete in the framework an NSNotification is sent back to the application. We have recently added a third party framework to our framework. Now, while the methods for the third party framework are being executed, as execution returns to our framework I receive the following error in the Console Output:
Assertion failed: (exclusive), function assert_locked, file ../dep/include/boost/boost_1_55_0/boost/thread/pthread/shared_mutex.hpp, line 51.
But I'm not sure that is the ultimate issue, because our framework continues to execute and the NSNotification is sent back to the application. Right after the Notification is sent and execution returns to the calling method (or the method call) in our framework I see a warning on the executing thread. Then, execution continues back to the original calling method and the warning goes away.
Here's the weird part. If I step through the code very slowly, it might just work. If I'm not slow enough I get the SIGSTOP and the code never returns to the UI. If I'm too fast, I get a SIGABRT.
I've been trying to find the exact issue using Instuments. This answer to a similar question backed up my suspicion that this is a timing issue. I think the boost assert_locked Assertion might have something to do with this.
My code is pretty boring but I know you want to see it, so here it is:
- (void)generateImageTemplates:(UIImage *)src
{
int result = 0;
cv::Mat image = *(cv::Mat*)[self cvMatFromUIImage:src];
user = IEngine_InitUser();
int userID=0;
result = IEngine_AddFingerprintRAW(user, UNKNOWN, image.data, image.size().width, image.size().height);
result = IEngine_RegisterUser(user, &userID);
[[NSNotificationCenter defaultCenter] postNotificationName:#"InnovatricsComplete" object:self];
}
If you're wondering what result is, it's an error code. So far these have all come back equal to 0. Meaning no errors. I'll work on handling these errors once I can get a successful return to the UI without crashing.
Control returns to the method call:
- (void)generateImageTemplates:(UIImage *)processedImage
{
[self.captureCommand generateImageTemplates:processedImage];
}
Control returns to the method call in the application View Controller:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
[self clearPressed:self.clearButton];
} else {
[self.cameraVC generateImageTemplates:self.processedImage];
}
}
Finally, the NSNotification callback code:
- (void)onInnovatricsComplete:(NSNotification *)note
{
[self.cameraVC willMoveToParentViewController:nil];
[self.cameraVC.view removeFromSuperview];
[self.cameraVC removeFromParentViewController];
}
I warned you it was pretty boring!
I'm completely stumped! Though I continue to surf the internet for clues, is there anybody out there who can help me resolve this issue?
Thank you.
Here are some screenshots (in reverse order):
look at NSUinteger’s answer in How to know where crash for postNotificationName:object:userInfo
the listener might be deallocated before it recieves the notification.

Connection Error Occurs When Browsing Too Fast

In the app that I'm writing, I check to see if the device has an internet connection. I put a connection error image over the screen, and hide it unless the device is not connected. There is an odd issue though. I implemented a simple back button for the UIWebView, but when I press it too fast, the connection error occurs. Here is the code I use to check for connection, and decide whether to display the error:
-(void)webView:(UIWebView *)myWebView didFailLoadWithError:(NSError *)error {
_connectionError.hidden = NO;
}
So, I think the only way to solve this issue would be to have it check if there is a connection one time, only when the app first launches, and never run again for the remainder of the time. I'm extremely new to Objective-C, and have no idea how to do this. I'm thinking that I should put something in viewDidLoad, or implement some way to have the method run only once, but I have no idea how to do that.
Here's the code for the back button:
- (IBAction)backButtonTapped:(id)sender {
[_viewWeb goBack];
}
Call the method stopLoading on the webView before the goBack method to make sure there is no multiple request going which can cause the connection error:
- (IBAction)backButtonTapped:(id)sender {
[_viewWeb stopLoading];
[_viewWeb goBack];
}
To check for a connection you can use Reachability in your project. You can then use this answer to see how to use it. This would be more efficient and cleaner than using a UIWebview.

IOS how to wait until `didReceiveData` handler is called

Im developing ios application which is getting data from the web server. I want everything else to wait, until one of the handlers of this class is called and completed. I know it is possible by using dispatch/threads, but i just can't figure out how.
-(void)callWebService:(NSString*)URL:(NSString*)SOAP{
NSURL *url = [NSURL URLWithString:URL];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:#"POST"];
[req setHTTPBody:[SOAP dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if(con){
[con start];
}
}
and at the end of this method continues code outside this class. but i want to wait until this handler is called (and completed):
-(void)connection:(NSURLConnection *)c didReceiveData:(NSData *)data{
NSString *res = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"%#",res);
Ukol_Parser *parser = [Ukol_Parser alloc];
[parser parseUkol:res];
}
because the parser here puts data into sqlite db and outside this class data are being read. But the "outside" code is being executed faster than i get response and handler is called....
If you want "everything else to wait", then it sounds like what you really want to do are synchronous requests.
Check out [NSURLConnection sendSynchronousRequest:returningResponse:error:]
However, make sure to do this thing on a background thread because if you do it on the main thread, your UI will block and your app will look unresponsive to user touches or anything else.
I'm nervous that you accepted the answer regarding sendSynchronousRequest from the background queue because, from a practical perspective, this is no different than your didReceiveData-based implementation. Specifically, if you do perform synchronous request from a background queue, it will not make "everything else wait".
But if you neglect to do this synchronous request from the background queue (i.e. if you do it from the main thread), you end up with a horrible UX (the app is frozen and the user is wondering whether the app has crashed), and worse, your app could be killed by the iOS "watchdog process" if it takes too long.
With all deference to the various answers, sending a synchronous request on a background queue is indistinguishable from the existing NSURLConnectionDataDelegate-based approach. What you really need to do is accept the fact that the rest of the app will not freeze, and therefore simply update the UI to let the user know what's happening, namely that (a) provide some visual cue that the app is not dead; and (b) prevent the user from interacting with your existing UI.
For example, before issuing your network request, add a view that will cover/dim the rest of your UI, prevent user interaction with the existing UI, and add a spinner to let the user know that the app is busy doing something. So, define a class property for a UIView that will dim the rest of the UI:
#property (nonatomic, strong) UIView *dimView;
And then, before the network request, update the UI:
// add a view that dims the rest of the UI, so the user has some visual cue that they can't use the UI yet
// by covering the whole UI, you're effectively locking the user out of using the app
// until the network request is done
self.dimView = [[UIView alloc] initWithFrame:self.view.bounds];
self.dimView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
self.dimView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.dimView];
// add a spinner that shows the user that something is really happening
UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicatorView.center = self.dimView.center;
indicatorView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[indicatorView startAnimating];
[self.dimView addSubview:indicatorView];
[self callWebService:URL withSOAP:SOAP];
And then, in the connectionDidFinishLoading method (or if you used sendSynchronousRequest from a background queue, in the latter portion of code to dispatched to that background queue), after you finish parsing your data, you want to:
[self.dimView removeFromSuperview];
self.dimView = nil;
// now do what ever to need do to update your UI, e.g.:
//
// [self.tableView reloadData]
But the key is that you absolutely do not want to issue a synchronous network request from the main queue. You should (a) do your network request asynchronously (either synchronously on a background queue, or as you originally implemented) so that the iOS watchdog process doesn't kill your app; (b) let the user know the app is making some network request and hasn't frozen on them (e.g. a UIActivityIndicatorView; and (c) when the request is done, remove these "the app is doing network request" UI elements and refresh the rest of the UI now that your network request is done.
Finally, when testing your app, make sure you test it in real-world networking situations. I'd suggest you install the Hardware IO tools (available in Xcode menu, under "Open Developer Tools" - "More Developer Tools") and check out the Network Link Conditioner. This lets you simulate real-world network situations (e.g. a bad 3G or Edge network condition) on the iOS simulator. We get lulled into a false sense of performance when we test our apps in typical development environments with ideal network connectivity. Devices "in the wild" suffer a wide range of degraded network situations, and it's good to test your app in a similar, suboptimal network situation.
kind of a wild solution, but this actually worked https://gist.github.com/62940 :D
use synchronous calls . but It'd change the design of your class because a synchronous call will block and leave the app hanging
Post a notification from your didReceiveData: method, and have your other class observe that notification (or you could use a delegate setup, if it's easy to get a reference to this class from the other so you can set your other class as the delegate of this one). In the notification's selector method, start executing the rest of your code.

Minimize didFinishLaunchingWithOptions Processes on launchOptionsLocation key discovery?

I have location processing newly implemented in my app, testing in foreground and background with satisfactory results. The application is monitoring significant location changes as well as several regions. I have not yet figured out if I will get the same results when the app is suspended or terminated.
As I understand it, when the app is woken from these states it will be as if the app just started except the launchOptionsLocation key will be found in the launchOptions Dictionary param. My question is, can I allow the app delegate to proceed as normal and assume all is well? Is it necessary to intercept all the view setup code?
The very first lines in my didFinishLaunchingWithOptions:launchOptions method are:
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
//Handle Error
}
self.sharedLocationHandler = [[[TTLocationHandler alloc] init] autorelease];
self.siteLogger = [[[ProjectSiteLogger alloc] initWithOptions:nil] autorelease];
self.siteLogger.locationHandler = self.sharedLocationHandler;
self.siteLogger.managedObjectContext = context;
In all likelihood, this covers all that I need in order to respond to the location event. I could easily test for the location key in launchOptions and skip the entire remainder of the method, though I am not sure what unforeseen complications that may entail.
I also question what would result then if a user happened to start the app intentionally while it was in that incomplete state having no views set up at all.
Is this something that has been tried, is it even necessary at all? I don't see how to test this as I don't know of a way to stay connected to Xcode debugger when the app is suspended.
---Additional Updated Info----
Initial testing on a day of carrying test phone around, my location processing seemed to do all the tasks I wanted it to with no changes to appDelegate. So I presume that wake from suspend/term came up and executed the full appDelegate procedure even though no view controllers ever became visible.
So, while it seems that it is not required to alter the startup procedures, may there still be a performance or battery concern that would make it prudent to cut the appDelegate procedure short and minimize processing?
After a good bit of testing and tweaking I find:
The app will wake from inactive or termed with no issues.
My location methods ran and completed regardless of the changes to the App Delegate.
When I cut the app delegate processes short, I did have intermittent start issues as anticipated.
Even though there were no apparent performance benefits, I went ahead and separated out the view setup code and added a flag so that it would not be run unless the application was being presented in the foreground. It just seems right to run no more processing than needed.
The code I ended up with is:
// Initialize a flag
BOOL needsViewsSetup = YES;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
// All my response to location events are handled in the these two classes
self.sharedLocationHandler = [[[TTLocationHandler alloc] init] autorelease];
self.siteLogger = [[[ProjectSiteLogger alloc] initWithOptions:nil] autorelease];
self.siteLogger.locationHandler = self.sharedLocationHandler;
self.siteLogger.managedObjectContext = self.managedObjectContext;
// check launchOptions, skip all the views if there we were woken by location event
if (![launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
[self confirmDataExistsAtStartup];
[self setupTabbarWithViews];
}
return YES;
}
Then you have the views setup:
-(void)setupTabbarsWithViews
{
// Code to setup initial views here
// ending with flag toggle to prevent repeat processing
needsViewsSetup = NO;
}
And in application will enter foreground:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
if (needsViewsSetup) {
[self setupTabbarWithViews];
}
}
Note: My application is not running location services in the background, only listening for significant location changes and geofence.

Resources