App Crashes when using pushing from UINavigationController, JSON TableView - ios

I am going to try to explain my issue the better I can.
So I have a UINavigationController. Inside I have a UITableView using JSON to get a list of arrays. When the user presses on a cell, it takes them to the detail view, where there I have a new view with information and UITableView that displays a new array from JSON. Each view has its own URL with different data. Now, the issue that the app crashes when I push on the detail view. It loads at first but then it crashes. If I remove the code to display the tableview and all the JSON stuff, it works just fine. The code is well written as far as I know. I created a whole new page using the same code of the first view where is the main tableview, and it still crashes. I have no idea why. From top of my head, it looks like the app doesn't allow me to use a new tableView with its data from the web using JSON but it does allow me to use it on my main view.
Ok, now, here are the codes I'm getting when it crashes (The codes are not always the same):
This the line where is crashing at:
cell.textLabel.text = [[news objectAtIndex:indexPath.row]objectForKey:#"Series"];
I get all these crashes each time I build the app over and over again:
1: THREAD : EXC_BAD_ACCESS (code=1, address=0x5000000c)
2: THREAD : EXC_BAD_ACCESS (code=1, address=0xc)
3: THREAD : EXC_BAD_ACCESS (code=1, address=0x41c8000c)
4: Thread 1: signal SIGABRT
And the console log:
and this one too:
And the codes keep coming on and on.
This is the code I use to call the URL:
NSString *urlString = [NSString stringWithFormat:#"http//MYURL.com/%#blahblah%d",fullDate, [[NSTimeZone localTimeZone] secondsFromGMT] / -60];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
Any ideas? Thank you!

Your news object is being deallocated prematurely. Check to make sure your keeping a strong reference to it (strong for arc, otherwise use retain). The memory that used to store your news is clearly being used by some CALayer now, and since CALayer does not respond to objectAtIndex:, your program is crashing. Use Zombies to find when your news object is actually being deallocated.

Related

Over-released object iOS app crash

My app is crashing with the last message in the device console:
objc[5105]: Cannot form weak reference to instance (0x10801ec00) of class UIPageViewController. It is possible that this object was over-released, or is in the process of deallocation.
Randomly app also crashed with the same error message but UINavigationController instead of UIPageViewController. A bug is reproducible on the simulator and physical devices (iOS 11.2.5).
Profiling with "Zombie" template in Instruments did not give any valuable information. I found that outdated guide useful:
http://www.corbinstreehouse.com/blog/2007/10/instruments-on-leopard-how-to-debug-those-random-crashes-in-your-cocoa-app/
My answer might not fix your issue but I hope it will help to track it. Additionally, I'll appreciate if someone explains why ARC fired on a different thread.
By subclassing UIPageViewController and setting breakpoint in it's dealloc I found that it's not executed on the main thread.
don't do this in production - only play around when debugging
Synchronously removing its view from view hierarchy on the main thread did fix the issue. I have no view controller container responsible for that:
- (void)dealloc {
dispatch_sync(dispatch_get_main_queue(), ^{
[self.view removeFromSuperview];
});
}
However it was not even about the view, while just delaying execution was enough to fix the crash, and the view will be cleaned together with its superview.
- (void)dealloc {
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(#"%#", self);
});
}
View controllers were stored in the array, and I knew that when this array was destroyed, view controllers are as well. I checked on which thread object holding the only one reference to this array is destroyed, and it is main thread. ARC should release objects on same thread where the last reference to the object was removed, but that is not the case in my app. https://stackoverflow.com/a/32800112
How did I fix my issue:
I don't why it was so. I did not search for a reason anymore when I found that issue will also disappear when I manually remove reference to the array, right before removing the last reference to its owner.

Delay in programmatically displaying a view controller

RESOLVED
I was performing the segue from inside a block and it was another thread. When I specifically move the operation back to the main thread it works great.
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self performSegueWithIdentifier:#"LoadToDisplay" sender:self];
}];
QUESTION
I want to programmatically present a view controller.
My app downloads some data from the internet, carries out some data handling and then loads the next view.
I use performSegueWithIdentifer successfully throughout the rest of the app however for some reason it is adding a huge delay with this specific transition.
I've used presentViewController with no delay but I can't use this, it was just to test whether I was missing something obvious.
I have an NSLog when the last data handling method completes and one when the next view controller is loaded. Using 'presentViewController' the time between logs is 14ms. When using performSegueWithIdentifer is a staggering 8.5secs!
I literally commented out one line and tested with the other. No other code changes.
Has anyone else experienced this or know what might be going on?
Thanks.

Loading hidden/offscreen UIWebView

Actually I have two related questions here, about different use cases of loading requests in a UIWebView.
Is it safe to call - [UIWebView loadRequest:] on a web view that is inserted in the view hierarchy and its hidden property or the one of its superview is set to YES?
Is it safe to call - [UIWebView loadRequest:] on a web view that is not inserted in the view hierarchy?
In particular I'm interested whether it is considered to be a good practice to load request in a UIWebView that is not visible, and whether the delegate assigned to the instance of UIWebView will be notified once the request succeeds/fails. The reason I'm asking is that UIWebView class reference says "create a UIWebView object, attach it to a window, and send it a request to load web content", where the part telling that a UIWebView should be attached to a window makes me doubt if the above approaches are reliable.
I have successfully used [UIWebView loadRequest:] with objects that are not in the view hierarchy. I expect that the class reference just assumes that the view will be displayed as it's probably the most common use case.
Yes it is safe to call [UIWebView loadRequest:] on a web view that is inserted in the view hierarchy.Because you are going to use web in the view.Also if you just give the URL in the program,it is enough to get.
The following code for [UIWebView loadRequest] is
NSString *strurl =#"http:www.google.com";
NSURL *url=[NSURL urlWithString:strurl];
NSURLRequest *urlrequest =[NSURLRequest requestWithUrl:url];
[webView loadRequest:urlrequest];
Even it is safe to call [UIWebView loadRequest:] on a web view that is not inserted in the view hierarchy.Because you can dynamically create the view and write the code for web view through the program.
It works. The approach is completely reliable as long as you are not using any private API and following HIG. It is not a bad practice as long it suits to your requirement. If there is a hidden property available to you for UIWebView then of-course you can hide the webView as per your requirements.
About your below query, it is written in the documentation as per the sentence context.
The reason I'm asking is that UIWebView class reference says "create a
UIWebView object, attach it to a window, and send it a request to load
web content", where the part telling that a UIWebView should be
attached to a window makes me doubt if the above approaches are
reliable.
The full context is below, which clearly means that to display a webpage in your application using UIWebView, you have to do it in the mentioned way.
You use the UIWebView class to embed web content in your application.
To do so, you simply create a UIWebView object, attach it to a window,
and send it a request to load web content.

switching screens and transition screens for an app in objective-c

This is a two part question.
The first question:
So I have an app that transitions from one screen to another, using nib files. heres the code i use to switch screens:
self.transitionView = [[TransitionViewController alloc] initWithNibName:#"TransitionViewController" bundle:nil];
[self.view addSubview: self.transitionView.view];
my problem is i have about 30 - 40 lines of code in the viewdidload method that could conceivably be slowing the transition down slightly, as some of the code uses NSURL to read data in, but its a 2 - 3 second wait between when it enters the viewdidload method and when the view actually finishes loading. the line [super viewdidload] is at the very top of all of this too, which is the most confusing part. so why is my program taking 2 - 3 seconds to load? i think thats a pretty long time considering how little code i have there.
part 2 of the question is i want to create a transition screen that appears for 2 - 3 seconds before the actual screen loads. Ive taken that code out and for some reason my nib file still takes forever to load so i know it has nothing to do with that, but the transition screen needs to pop up BEFORE screen 2 for 3 seconds, even if the app is being opened after 10 minutes. i know there was a function in appdelegate that could assist in this endeavor but im at a loss for what the code should look like. it doesnt seem to be switching with
self.transitionView = [[TransitionViewController alloc] initWithNibName:#"TransitionViewController" bundle:nil];
[self.view addSubview: self.transitionView.view];
and aside from that, im at a loss for an understanding about how the code should be constructed to switch between screens in that fashion. any articles or suggestions anybody can give me?
EDIT
I also tried to separate the NSURL data in the viewdidload method from the method itself. i put it all in a method called initialize, and called it from screen 1 right after calling the 2nd view. it didnt help the switching time at all. the reason i did that is because i didnt want to receive all the nsurl data over again when the person opens the app. i just want them to retrieve it one time and be done with it. am i doing somethign wrong here? this is extremely frustrating
You should not put your transition effects in viewdidload , you should put them in viewdidappear or viewwillappear, that is the the next step that the controller will take after it is loaded. That is your best choice, as for the screen to to put it there, you need to load it using the nib file or in the viewdidload.
I hope that was helpful

Web View is not reloading every time in iPad web view?

First thanks in advance to have a look on my issue. My issue is my am developing webview based application. It's same like as browser but i have a issue related to reload web view. When i am getting a value form popover bookmarked and try to reload the current web view from this next bookmark url, my web view do noting. I am stuck in this issue. Although when i enter any url through it works perfectly. May i doing something wrong with web view ?
temp = appDeleg.strURLFromPopover;
NSURL *url = [NSURL URLWithString:temp];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:requestObj];
[self.myWebView setDelegate:self];
[self.view addSubview:myWebView];
Please have a look..
The last line looks like very suspicious. In my experience, [self.view addSubview:myWebView] should be placed in the "initialization block" to avoid some lazy init issues. Are you sure All objects are NOT nil? if so, you can try to set UIWebView's delegate to your class, and debug in each method to test if your object are properly initialized. I tried your code, work fine in my side. Below are delegate method you might consider about
– webView:shouldStartLoadWithRequest:navigationType:
– webViewDidStartLoad:
– webViewDidFinishLoad:
– webView:didFailLoadWithError:

Resources