how to cancel the background running thread in my iphone app - iphone

i wrote like:
[self performSelectorInBackground:#selector(backgroundImageLoading) withObject:nil];
to load image background but know i need to stop this when i refresh the view but i don't have any idea how to do

See NSOperation and NSOperationQueue.
Its also to do the process concurrently as like background thread. Here in NSOperation you have cancel method available to stop the operation.

The threads should not be forced to kill. look out the reasons here. It may help you to find the solution.

Related

Modal alert in background thread in iPhone

In my iPhone app, main thread creates a background thread which does a lot of work and in some cases needs to ask user for their decision. When user is asked the question, background thread should stop working and should continue only after user has answered question.
What is the best way to do it?
P.S.
I've tried ModalAlert from iPhone Developer's Cookbook, which was said to do the trick, but I have not succeeded. It seems like CFRunLoopRun (which is supposed to stop thread from executing) just does not stop the background thread.
I had code like that
+(void) main
{
[NSThread detachNewThreadSelector:#selector(startSync) toTarget:self withObject:nil];
}
+(void) startSync
{
CFRunLoopRun();
NSLog("Hi");
}
and NSLog was just immediately executed after start of syncStart thread. Weird.
Have you tried NSObject's performSelectorOnMainThread:withObject:waitUntilDone: method?
Present your question to the user on the main thread, then store the response somewhere where your background thread can access it. Also, you should be able to block your background thread for the duration that your main thread selector runs by setting YES to the waitUntilDone: parameter.

UIActivityIndicator starting loop after the execution of the process

I am trying to get my UIActivityIndicator start animating when the process of data consumption start downloading. My concern is: how do you get the activity indicator start animating at the same time? I have gone through some solutions here on Stack Overflow, yet I am trying to find some universal solution, since I am utilizing a subclass of UIActivityIndicator. I am not sure if a separate thread can resolve this issue?
I used:
[self performSelector:#selector(methodname) withObject:nil afterDelay:0];
However, this didn't solve my problem.
Just return from the method starting the activity indicator. The UI (including the activity indicator) updates after an app returns to the main run loop. Handle any long activities (such as your download) asynchronously with delegates or callbacks, so that the activity indicator can continue to run. So return from the method that starts your download as well. Finish later in another method.
Another option is to do the download, or other long process, in another thread. Operation queues and blocks can be used for this purpose. Use a performselector on the main thread to update the UI, as an app can't update the UI directly from within a background thread.

How to implement NSThread to constantly perform the function in background in iPhone SDK

In my iPhone app, I have to perform function constantly in background.
For that I think I will have to use NSThread to call the function and keep it executing in background.
I dont want to stall my app and hence I want to use NSThread to keep my Main Thread free for user interaction.
How should I implement NSThread to perform the function in background?
EDIT:
The function is for fetching the data from a web server every 20 seconds and updating the tables in my iPhone app based on the data that is fetched from the web server.
I'd look at an NSOperationQueue first.
I'm guessing that your background task is really a small task repeated again and again. Make this into an NSOperation subclass and just add them onto an NSOperationQueue. That way you can control the background tasks more easily.
You also get the advantage with an NSOperationQueue that when there are no operations to run, the processor isn't just stuck in a while(YES) loop, waiting. This will help your app's UI be more responsive and will help battery life :)
However, if your background task is a single long running task that just needs to be started and then ignored, performSelectorInBackground isn't too bad an idea.
Sounds like a bad idea, but it's very simple.
[self performSelectorInBackground:#selector(theMethod:) withObject:nil];
Just have a while(YES) in theMethod: and it will never stop executing.
EDIT:
Luckily for you it's just as simple to do something once every 20 seconds.
[NSTimer scheduledTimerWithTimeInterval:20 target:self selector:#selector(theMethod:) userInfo:nil repeats:YES];
This will execute theMethod: once every 20 seconds. I might also add that this is a much better idea.
you'll want to interact with the thread's run loop. if it's an NSThread, it is created automatically. thus, you are given access to the CF/NS-RunLoop - typically by calling + [NSThread currentRunLoop] from the secondary thread.
in that case, you add a CF/NS-Timer to the run loop, and let it run and repeat until your work is finished. when the timer fires, your thread is awoken, and you then do your work.

show uiactivityview while an other operation is loading

i googled for hours now but i can't find a code example, which describes how to show an uiactivityview while an operation/method is loading.
in my case i want to show it while i load a large image to an uiimageview. have you got an idea how to realize that?
thanks in advance.
sean
You might want to look in the UI UIActivityIndicatorView Class Reference documentation.
Make sure to return to the run loop while waiting for the image to load in order to see the activity indicator animating. Also, you will probably need to create a method to stop the activity indicator, and perhaps call that stop method by doing a performSelectorOnMainThread from whatever method handles the asynchronous completion of the loading.
well its quit easy.
show activity indicator
start thread loading that image
if image loaded, thread tells mainapp to kill activity indicator.
NSThread detachNewThreadSelector:#selector(myMethod)
toTarget:self
withObject:nil];
and
[self performSelectorOnMainThread:#selector(myMethod)
withObject:nil
waitUntilDone:false];
will be helpfully.
taken from http://www.iphoneexamples.com/

Activity indicator not animating=iphone

In my application i am uploading some images to the servelet, which takes some time to complete upload. I have implemented an uiactivityviewer using IB and connected it to an outlet named acticityIndicator. But when i call
[activityIndicator startsAnimating];
nothing is happening!!!
What might be the problem.
i've debugged the code,
the statement is executing, but nothing happens!!!
Are you performing your uploading on the main thread, or kicking off a background thread to do it?
If the former then you are blocking the UI thread so it doesn't have a chance to update the activity indicator.