Threading in iphone - iphone

I m using NSoperationQueue for load song from server.
This code execute when click on diff-diff buttons.
NSOperationQueue *queue;
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self selector:#selector(loadImage)object:nil];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[queue addOperation:operation];
Problem is that when user click ie.Rama.aac then loading continue in this duration if he click krishna.aac then this process also goes into that queue.and conflicting is there.
user is finally requesting for krishna but in result first download rama.aac then krishna.aac.
I m using [queue cancelAllOperations] but it not works.
How i solve this?

Implement your own NSOperation subclass. NSOperation has isCancelled property set when cancelAllOperations is called on the queue.

If I'm not mistaken using NSOperations to load data from a network is not advised. You should use the asynchronous loading NSURLConnection provides.

Related

perfomSelectorOnMainThread:withObject:waitUntilDone: randomly stops working

I'm running a selector on the background thread then I need to call the main thread and it works perfectly, but when there are a lot of selectors running on the background and they try to call a selector on the main thread sometimes it gets called, sometimes it doesn't. I can see the code is getting there because I'm printing with NSLog();
This is how I call the selector on the background:
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:#selector(getPath:)
object:datos];
[queue addOperation:operation];
[operation release];
And this is how the background method calls the main thread method:
NSLog(#"Arrives here");
[self performSelectorOnMainThread:#selector(setPath:) withObject:array waitUntilDone:YES];
Why does it sometimes work, and sometimes it doesn't?
How do you know it doesn't work?
Is there anything blocking your main thread? A modal session, per chance?
Note that "a lot of selectors running on the background" sounds scary; un-throttled concurrency is pretty much guaranteed to be the wrong answer. It is quite easy to create a system that storms the main event loop with so much noise that it looks like events are being dropped.

How to use NSOperationQueue to download audio files from server one by one

I have an array containing audio file url's. I want to fetch audio files from server using these url's in background mode. I have heard that i can achieve this with NSOperationQueue. My query is
1)How can i achieve this.
2)How can i get call back on single operation completion/failure
3)How can i get call back after completion of the whole process.
I need these call backs to keep track of downlaod process so that i can update my database about the download status of files. So, in case any internet connection loss i can download the remaning files again.
Any idea will be helpful as i am new to NSOperationQueue.
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:#selector(download:) object:aAudio];
[queue addOperation:operation];//added code
[operation release];
now do stuff what u want in method download. as per doc set [queue setMaxConcurrentOperationCount:1] for one by one.
Seems like AFNetworking has all that you need (callback blocks for success / failure, puttings requests in NSOperationQueue). In your case probably AFHttpClient and its enqueueHTTPRequestOperation method will do the job.

setDelegateQueue is not working on iOS5 but working fine on iOS6

I am trying to perform a downloading in a background thread.For this, I have created an instance of NSOperationQueue and add an instance of NSInvocationOperation to it.
operationQueue = [NSOperationQueue new];
// set maximum operations possible
[operationQueue setMaxConcurrentOperationCount:1];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:#selector(startBackgroundThread:)
object:data];
[operationQueue addOperation:operation];
[operation release];
Now in startBackgroundThread,I have created a new NSURLConnection and dispatching the operationQueue to it.
currentConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
if (self.operationQueue) {
[currentConnection setDelegateQueue:self.operationQueue];
}
[currentConnection start];
Behavior in iOS6
Working exactly fine on iOS6.
A thread gets created and NSURLConnection callbacks it delegates on the same background thread.
Behavior in iOS5 and iOS5.1
A thread gets created but NSURLConnection delegates didn't get called.
Am i missing something that need to be taken in account in iOS5?
I have read the documention provided by Apple but nothing is mentioned there related to that.
This is a known issue on iOS 5. See http://openradar.appspot.com/10529053
If you are trying to perform a background web request then use the following convenience method.
[NSURLConnection sendAsynchronousRequest: queue: completionHandler:];
Block apis give better performance and recommended by apple.

NSURLConnection threading problem

i have a big problem and i need your help. Here's what i need to accomplish:
The user select a row from a
TableView
A new view controller is pushed in
the NavigationController, and
displays only a "Loading" message
Meanwhile some data is read from an
XML file (via http)
When the data has been read, an
NSUConnection is used to load an
image from an URL (this URL is part
of the data)
While the image is still loading,
the other data is displayed on the
screen
The image has been downloaded and is
shown, completing the appearance of
the view
The big problem is that i can't use detachNewThreadSelector and NSURLConnection together!
So how can i make a workaround for this? How would you do this?
Thank you VERY much!
You can use following approach...(if you are using asynchronous request)
When your application comes in - (void)connectionDidFinishLoading:(NSURLConnection *)connection ... add a NSInvocationOperation object in NSOperationQueue (which you can handle at application level, by synthesizing it in appDelegate) ..
create NSInvocationOperation as follows..(in connectionDidFinishLoading)
NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:#selector(parseIt) object:nil];
[appDelegate.operationQueue addOperation:operation];
[operation release];
-(void) parseIt
{
//ask for parsing stuff....what you have earlier wrote directly in connectionDidFinishLoading
}
Thanks,
I would use a NSTimer to solve the problem using detachNewThreadSelector and NSURLConnection together.
I have similar scenario where there is a downloading Progress UIViewController showing till the file getting complete, here is what i do:
I Draw a loading View contains a Activity Indicator for example.
I initialize a NSTimer to keep checking if the file is complete.
I call the method that contains the Download Logic.
[1]
-(void) vManageFileRequest
{
[self.oFilesManager vGetSingleFileRequest];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(vValidateFileRequest) userInfo:nil repeats:NO]];
}
[2]
[self performSelectorOnMainThread:#selector(vManageFileRequest) withObject:nil waitUntilDone:NO];

Why is my NSOperationQueue not behaving correctly in iOS 4.0?

I have used NSOperationQueue in my iPhone app before in iPhone OS 3.0, but now in iOS 4.0 the code is not working properly. It runs properly only once and on all subsequent calls, it doesnt work. Have there been changes in NSOperationQueue in iOS 4.0?
The relevant code is as follows:
- (void) starteffectFunction {
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:#selector(starteffectProcessing)
object:nil];
[queue addOperation:operation];
[operation release];
[queue release];
[spinner startAnimating];
}
-(void) starteffectProcessing{
some code executes. code snippet. A
......
this code is note supposed to execute before A completes. But this executes before A.
}
You are creating an NSOperationQueue, adding an operation to it, then releasing the queue. This is not how NSOperationQueues were designed to work. An NSOperationQueue is supposed to persist, with you adding operations to it as necessary.
This is probably failing because you are deallocating the NSOperationQueue before it has a chance to fire off a thread for your operation. Perhaps on the older OS versions it was just able to do this due to some timing quirk.
I recommend allocating the effect processing queue when you first need it, or in the initialization of your controller object, then keeping that queue around as an instance variable of your controller object. This queue would be deallocated at the same time as your controller object, but you will probably want to cancel all current operations at that time and use NSOperationQueue's –waitUntilAllOperationsAreFinished method to make sure that you are completing all work before deallocation.