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
});
}
Related
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.
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.
In my app I have a login view. When I enter login details and click login button, the app freezes for some time and then continues. I know it's freezing because it is communicating with remote server but how can I prevent the freeze thing and show a nice looking status animation instead?
As you are communication with server it will takes some time, so meanwhile you can add some animation on alertview.
But by calling simply alertview it will not work.
So you have to proceed in the following steps:
1) On Submit Button Click first add Custom Indication View
2) Start Animation View
3) Call NSThread for server communication like
[NSThread detachNewThreadSelector:#selector(yourActionName) toTarget:self withObject:nil];
4) Remove Animation View
For more information go through this article:
http://iphonedevcentral.blogspot.com/2010/08/safe-threaded-design-and-inter-thread.html
Your API call is blocking the GUI thread. You can circumvent this by moving the remote API request call in a NSOperation call, and pop up a loading screen until the API call returns.
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.
I have a view with a button which downloads files when clicked.
Question I have is is it possible to call that click method from another view>
THanks
You have a method there. Something like onBtnClk. You can create in another view an instance of your viewController, that contains this method and send [myViewController onBtnClk].
This may be a good case to have some other class(maybe a singleton?) handle downloads, then have your click: method interact with the downloading class.
I know that everyone hates singletons, but this may be a good time to use one.