I have an application in which I have a settings page, where I need to edit each entity. For that when the user selects each entity we will go to a separate page and edit there and come back. I need to update the edited value by calling the service in background, but I need to come back to main view as soon as I edit the value. So I am using the GCD way.
dispatch_async(backgroundQueue_, ^{
[self update];
dispatch_sync(dispatch_get_main_queue(), ^{
});
});
But sometimes I get the crashes like ASHTTPheaders didreciveresponseheaders,and ASHTTprequest reportFailure. Can anybody point me in the right direction to achieve this.
If you want it to happen immediately after the app goes to the background, you can probably use -[UIApplication beginBackgroundTaskWithExpirationHandler:]. That should work as long as the task doesn't take too long.
Related
I have called a web service method to insert some data and uploading some images to web url.
It is working fine but the button remain pressed till the method perform.I have used threading concept here and created one another thread and perform that action but that thread is not taking me back to the main thread.
Please till me what would be the appropriate way of doing this.
The web service calling mechanism place in one seperate method and call that method from the IBAction of the button using the performSelector:withObject:andDelay () method. This will solve your problem.
Create one another function in which you can put the code for the webservice call so that your button press issue will be solved.
Also you can implement this using GCD.
- (IBAction)buttonPress:(id)sender
{
dispatch_async(dispatch_get_global_queue(0,0), ^{
//place your web service code here
});
}
im using a UIWebView to show html content in my app, the app contains two arrows to navigate between topics just like a RSS reader app, but when the user hits up or down arrow, the next topic doesn't show up until the data come back and the user still able to interact with the UI which is a bit confusing,
My question: how to block the UI when user moves to the next/back topic ? in other words how to make loadHTMLString:baseURL: works as a synchronous calling ? thanks !
You can let the load happen asynchronously, but set the web view's userInteractionEnabled property to NO. (then back to YES, on the didFinishLoad callback).
Or you could put up a clear colored view (with userInteractionEnabled set to NO) above the web view that has an activity indicator and button that lets the user cancel the load.
An even better idea would be to place two other web views offscreen and start loading them for page N-1 and N+1. When the user presses a page arrow, swap frames with the corresponding prefetched web view.
Try this - https://github.com/gavrix/UISynchedWebView-demo
You don't actually want to block the UI. If you do that, there is a very high probability that Apple will reject your app once you send it in for app store submission. Anything that even remotely makes the application feel unresponsive will weigh heavily on you. Instead, create a background thread using GCD or performSelectorInBackground, handle your loading in that, and then once the loading is done, make all of the information available to your UIWebView all at once and alert it to render the display.
If you are in a pinch and have a UIPageViewController and still want to use a UISynchedWebView to ensure that a page has loaded before you run javascript, you can run a block on the main thread's event queue. Still has a slight delay while the javascript runs but won't cause recursion in the run loop.
dispatch_async(dispatch_get_main_queue(), ^{
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com"]]]; // self.webView is a UISynchedWebView
NSLog(#"url:%#", [self.webView stringByEvaluatingJavaScriptFromString:#"window.location.href;"]); // shows google.com instead of about:blank
});
My iPhone app has a sub view on its Welcome View Controller. The sub view parse data from a website and load data accordingly. Welcome View Controller has a continue button to go to the next view controller. But until the sub view load its data I cannot go to the next view controller.
Can anyone suggest me any solution on this. Thanks in Advance.
In you are using NSURLConnection/NSURLRequest to retrieve the data, I would suggest two approaches:
modify the code your class that retrieves the data so that the request is made asynchronous; this will make it non-blocking and the user will be able to move next without waiting; when moving next, you'll have the option of canceling the request, so to save bandwidth;
perform the request in a separate thread; you can do that either using NSTask or GCD dispatch_asinc like shown below; in this case anyway, you have to be aware of the fact that your separate thread may not modify the UI (i.e., use UIKit), because this can only be done from the main thread. So, in your thread, you update the data, but then issue a refresh of the UI on the main thread (by using performSelectorOnMainThread).
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ [self SENDREQUEST]; });
As Vince pointed, you should retrieve the data from a separated thread (create a worker class to do that for you). When the classes finishes his job, you should tell the Welcome view, that the data is ready. You could achieve that, by using a protocol, or NSNotification.
I have a UIView that I want to load when the user clicks a button. There happens to be some data processing that happens as well after I call addSubview that involves parsing an XML file retrieved from the web. The problem is the view doesn't show up until after the data processing even if addSuview is called first. I think I'm missing something here, can anyone help?
Code: I have a "Loading..." view I'm adding as a custom modal (meaning I'm not using the modalViewController). This action is linked to a button in the navigationController.
- (IBAction)parseXml:(id)sender {
LoadingModalViewController *loadingModal = [[LoadingModalViewController alloc] initWithNibName:#"LoadingModalViewController" bundle:nil];
[navigationController.view addSubview:loadingModal.view];
[xmlParser parse];
}
Howdy! If you're looking for an easy work around:
[self showLoadingScreen]
[self performSelector:#selector(methodToDoWork) withObject:nil afterDelay:0.3];
However you're better off making methodToDoWork asynchronous if you can.
If you are doing your processing on the main thread, it will block the main thread until its done, which means your UI will become unresponsive and not update until the main thread resumes.
You need to perform your XML processing on a background thread using something like NSOperation or an existing asynchronous API and update your view when you have finished.
Its hard to be of more help and get a better idea of whats going wrong without seeing your code unfortunately.
I am creating a tabbar application. One of the tabs is for an rss feed, which is a navigation application. but when i click the tab bat button, it is taking a while to load the view of that tab. It is because the application is waiting for the feed to be loaded from the server. Is there any way to load the view before the loading of that feed takes place. As of now, i'm giving the request in the viewDidLoad method. Thats what is creating the problem. To which part shall i move the code so that the view is loaded instantaneously when clicking the tabbar button.
I recommend this great article on this subject on iCodeBlog, it's a very elegant way of doing this. If you submit your rss feed loading as an NSOperation, it will take place nicely in the background without blocking your main thread.
use:
[self performSelector:#selector(performRSS:) withObject:<nil afterDelay:0.3f];
or
[NSThread detachNewThreadSelector:#selector(performRSS:) toTarget:self withObject:nil];
and place RSS feed related code in a separate function named "performRSS".
I also think that the problem is more that you don't use the HTTP request asyncronously (as Apple recommends). See this document. http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
It worked for me in my applications.