I load a UIImageView using an NSOperationQueue.
The load fetches an image from the Internet and then adds it to an image view. The problem I have is that the method finishes but it takes about 3 seconds later for the image view to actually either show the image or remove the image view from the superview...
- (void)viewDidLoad { NSLog(#"AirportDetailView: viewDidLoad");
[super viewDidLoad];
[self.activity startAnimating];
self.queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:#selector(loadImage) object:NULL];
[self.queue addOperation:operation];
[operation release];
}
-(void)loadImage {
[myAp ImageForAp];
NSLog(#"ImageFor Ap Ended, %#",myAp.ApDiagram);
[self.activity stopAnimating];
if (myAp.ApDiagram==NULL) {
NSLog(#"Finally Gets Here");
[self.Diagram removeFromSuperview];
}
else {
NSLog(#"Finally Gets Here with Diag");
[self.Diagram setBackgroundImage:myAp.ApDiagram forState:UIControlStateNormal];
}
}
The NSLOG shows a delay between the first two log statements of about 3 seconds can't understand why....
Updated with my Latest Code......
-(void)loadImage {
[myAp ImageForAp];
NSLog(#"ImageFor Ap Ended, %#",myAp.ApDiagram);
[self performSelectorOnMainThread:#selector(UpdateUI) withObject:nil waitUntilDone:NO];
}
-(void)UpdateUI {
[self.activity stopAnimating];
if (myAp.ApDiagram==NULL) {
NSLog(#"Finally Gets Here");
[self.Diagram removeFromSuperview];
}
else {
NSLog(#"Finally Gets Here with Diag");
[self.Diagram setBackgroundImage:myAp.ApDiagram forState:UIControlStateNormal];
}
}
Make sure that the loadImage method is being run on the main thread. All UI operations need to happen on the main thread to work as expected. Your code will need to look similar to this.
-(void)loadImage {
[myAp ImageForAp];
[self performSelectorOnMainThread:#selector(UpdateUI) withObject:nil waitUntilDone:NO];
}
-(void)UpdateUI {
[self.activity stopAnimating];
if (myAp.ApDiagram==NULL) {
[self.Diagram removeFromSuperview];
}
else {
[self.Diagram setBackgroundImage:myAp.ApDiagram forState:UIControlStateNormal];
}
}
There is also a possible memory leak inside of viewDidLoad.
self.queue = [NSOperationQueue new]; should be changed to
self.queue = [[[NSOperationQueue alloc] init] autorelease];
Related
- (void)viewDidLoad {
[super viewDidLoad];
[self pingSplash];
UIViewController *next = [[self storyboard] instantiateViewControllerWithIdentifier:#"ViewController"];
[self.navigationController pushViewController:next animated:YES];
}
I mean to finish pinSplash then pushViewController, but it directly goto ViewController page, even without finishing pingSplash, what is a good way to do that kind of job?
For the pingSplash part:
- (void) pingSplash
{
SKSplashIcon *pingSplashIcon = [[SKSplashIcon alloc] initWithImage:[UIImage imageNamed:#"ping.png"] animationType:SKIconAnimationTypePing];
_splashView = [[SKSplashView alloc] initWithSplashIcon:pingSplashIcon backgroundColor:[UIColor grayColor] animationType:SKSplashAnimationTypeBounce];
_splashView.animationDuration = 5.0f;
[self.view addSubview:_splashView];
[_splashView startAnimation];
}
The pingSplash method returns as soon as it starts the animation which is why you end up pushing the view controller too soon.
There are a few ways to solve this. One way would be to pass a block to the pingSplash method that is run after an appropriate delay.
Here's one way:
Update the pingSplash method to take a completion handler block that is run after the 5 second delay.
- (void) pingSplash:(void (^)(void))completion
{
SKSplashIcon *pingSplashIcon = [[SKSplashIcon alloc] initWithImage:[UIImage imageNamed:#"ping.png"] animationType:SKIconAnimationTypePing];
_splashView = [[SKSplashView alloc] initWithSplashIcon:pingSplashIcon backgroundColor:[UIColor grayColor] animationType:SKSplashAnimationTypeBounce];
_splashView.animationDuration = 5.0f;
[self.view addSubview:_splashView];
[_splashView startAnimation];
if (completion) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_splashView.animationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
completion();
});
}
}
Then update your viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
[self pingSplash:^{
UIViewController *next = [[self storyboard] instantiateViewControllerWithIdentifier:#"ViewController"];
[self.navigationController pushViewController:next animated:YES];
}];
}
how in order load music, images, and other stuff to scene with progress bar and move bar smooth..
i guess logic of progress bar is to create new thread - load data and destroy thread
here is my code to load stuff but it's not work, progress bar appears but not updating value
-(void)s1
{
[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:#"game_music.caf"];
}
-(void)s2
{
[[SimpleAudioEngine sharedEngine] preloadEffect:#"tap.caf"];
}
-(void)startThread
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
EAGLContext *context = [[[EAGLContext alloc]
initWithAPI:kEAGLRenderingAPIOpenGLES1
sharegroup:[[[[CCDirector sharedDirector] openGLView] context] sharegroup]] autorelease];
[EAGLContext setCurrentContext:context];
[self performSelector:#selector(loadBar)];
//[self schedule:#selector(tick:)];
[self performSelector:#selector(s1)]; // uploading file
[self performSelector:#selector(progressUpdateValue)]; // add 10 value to progress
[self performSelector:#selector(s2)]; // uploading file
[self performSelector:#selector(progressUpdateValue)]; // add 10 value to progress
[self performSelector:#selector(replaceScene)
onThread:[[CCDirector sharedDirector] runningThread]
withObject:nil
waitUntilDone:false];
[pool release];
}
-(void)replaceScene
{
[[CCDirector sharedDirector]replaceScene:[GameScene node]];
}
-(id)init
{
self = [super init];
if (self != nil)
{
[NSThread detachNewThreadSelector:#selector(startThread) toTarget:self withObject:nil];
}
return self;
}
Thanks in advance.
interface.. there you go..)
#interface LoadScene : CCScene
{
GPLoadingBar *loadingBar;
float value;
}
Ok, so you should load resources in background, while updating loadBar in main thread. For the SimpleAudioEngine you really need NSThread, but CCTextureCashe has -addImageAsync method which allow you to load images asynchronously without problems. So your code should look something like this:
-(void)s1
{
[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:#"game_music.caf"];
}
-(void)s2
{
[[SimpleAudioEngine sharedEngine] preloadEffect:#"tap.caf"];
}
-(void)startThread
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self s1];
loadingBar.value = 0.5;
[self s2];
[self performSelectorOnMainThread:#selector(replaceScene)
withObject:nil
waitUntilDone:false];
[pool release];
}
-(void)replaceScene
{
[[CCDirector sharedDirector]replaceScene:[GameScene node]];
}
-(id)init
{
self = [super init];
if (self != nil)
{
[self loadBar];
[NSThread detachNewThreadSelector:#selector(startThread) toTarget:self withObject:nil];
}
return self;
}
-(void) loadingFinished
{
[self replaceScene];
}
Cocos2d has its own updating loop, so you don't need to create your one for updating loadingBar. It also has a drawing loop, so if you want to dynamically update something on screen you should only set up values for update and not to stop main thread with loading resources
also, you could use [self s1] instead of [self performSelector:#selector(s1)] because they are identical. Hope that would help
got this code:
[self setQRCodeScannerMode:false];
[self.activityIndicator startAnimating];
TBXMLSuccessBlock successBlock = ^(TBXML *tbxmlDocument) {
if ([[TBXML elementName:tbxmlDocument.rootXMLElement] isEqualToString:#"xxxxxx"]){
[self setQRCodeScannerMode:true];
} else {
[self setQRCodeScannerMode:true];
}
};
The thing is that when I set my scan mode to true inside the method [setQRCodeScannerMode] I'm stopping the activity indicator.
But surprise!!! the activity indicator is still working and messing with my view after some seconds.
What can I do?
You need to stop the activity indicator in your completion block:
[self setQRCodeScannerMode:false];
[self.activityIndicator startAnimating];
TBXMLSuccessBlock successBlock = ^(TBXML *tbxmlDocument) {
if ([[TBXML elementName:tbxmlDocument.rootXMLElement] isEqualToString:#"xxxxxx"]){
[self setQRCodeScannerMode:true];
} else {
[self setQRCodeScannerMode:true];
}
[self.activityIndicator stopAnimating];
};
This assumes the completion block is called on the main thread. If there is no guarantee that the completion block is called on the main thread you can do this:
[self setQRCodeScannerMode:false];
[self.activityIndicator startAnimating];
TBXMLSuccessBlock successBlock = ^(TBXML *tbxmlDocument) {
if ([[TBXML elementName:tbxmlDocument.rootXMLElement] isEqualToString:#"xxxxxx"]){
[self setQRCodeScannerMode:true];
} else {
[self setQRCodeScannerMode:true];
}
if ([NSThread isMainThread]) {
[self.activityIndicator stopAnimating];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityIndicator stopAnimating];
});
}
};
You are supposed to call [self.activityIndicator stopAnimating]; after you are finished with it, if thats not working for you, try this!
[self.activityIndicator performSelector:#selector(stopAnimating) withObject:nil afterDelay:0.1];
Hope that Helps!
One thing that you can do is working with the indicator on a different thread i.e. not on the Main Thread. You can use NSOperationQueue and NSInvocationOperation
simply add this code after the block
[self.activityIndicator stopAnimating];
-(void)startThread {
m_bRunThread = YES;
if(m_bRunThread) {
NSThread* myThread = [[NSThread alloc]initWithTarget:self selector:#selector(display) object:theConditionLock];
[myThread start];
/*((WaitForSingleobject(event,1500) != WAIT_OBJECT_O) || !m_bRunThread) {
m_bRunThread = false;
Trace(_T("Unable to start display Thread\n"));
}*/
}
[self insert];
}
-(void)insert {
[theConditionLock lockWhenCondition:LOCK];
NSLog(#"I'm into insert function of thread!");
[theConditionLock unlockWithCondition:UNLOCK];
}
-(void)display {
NSLog(#"I'm into display function");
while (YES) {
[theConditionLock lockWhenCondition:LOCK];
NSAutoreleasePool* pool1 = [[NSAutoreleasePool alloc]init];
NSLog(#"Into the lock");
[theConditionLock unlockWithCondition:UNLOCK];
[pool1 drain];
}
}
Both the insert and the display methods are called from the startThread.display is called before the calling of the insert method.But i want the display to wait till the insert finishes its execution.And if its stopped a signal has to be sent to the start thread to display the error message.
How to do it.
But in the code above the display method is called first and it continues in the infinite loop.
If you want to call insert before display simply move it above the call to start the thread.
m_bRunThread = YES;
[self insert];
if(m_bRunThread)
{
NSThread* myThread = [[NSThread alloc]initWithTarget:self selector:#selector(display) object:theConditionLock];
[myThread start];
}
you could try: performSelector:onThread:withObject:waitUntilDone: on self
From my understanding you want to sync the insert and display threads, such that display is called after insert (startThread), and report back to insert (startThread) (not sure if I got it right).
If I hot it right, something like this should do the trick (not tested, might need some small changes):
[self insert];
NSThread* myThread = [[NSThread alloc] init];
[self performSelector:#selector(display) onThread:myThread withObject:nil waitUntilDone:YES];
//myThread stopped, check its return status (maybe set some variables) and display error message
this would be another way:
m_bRunThread = YES;
[self insert];
if(m_bRunThread)
{
NSThread* myThread = [[NSThread alloc]initWithTarget:self selector:#selector(display) object:theConditionLock];
[myThread start];
}
while(m_bRunThread){//check if display is still running
[NSThread sleepForTimeInterval:0.5];
}
//display thread stopped
I'm trying to add a spinning activity indicator (UIActivityIndicatorView) to my app while it parses data from the internet. I have an IBOutlet (spinner) connected to a UIActivityIndicatorView in IB. Initially I had it set up like this:
-
(void) function {
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
self.spinner.hidesWhenStopped = YES;
[spinner startAnimating];
//parse data from internet
[spinner stopAnimating];}
But the spinner wouldn't spin. I read that it had something to do with everything being on the same thread. So I tried this:
- (void) newFunction {
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
self.spinner.hidesWhenStopped = YES;
[spinner startAnimating];
[NSThread detachNewThreadSelector: #selector(function) toTarget: self withObject: nil];
[spinner stopAnimating];}
But still no luck. Any ideas? Thanks.
Your newFunction: method should look like this:
- (void) newFunction {
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
self.spinner.hidesWhenStopped = YES;
[NSThread detachNewThreadSelector: #selector(function) toTarget: self withObject: nil];
}
And your function method should look like this:
- (void) function {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self.spinner performSelectorOnMainThread:#selector(startAnimating) withObject:nil waitUntilDone:NO];
//...
[self.spinner performSelectorOnMainThread:#selector(stopAnimating) withObject:nil waitUntilDone:NO];
[pool drain];
}
you should not intitialize indicator again .please replace your code with this.
-(void) function {
[spinner startAnimating];
[self performSelector:#selector(newfunction) withObject:nil afterDelay:3.0];
}
- (void) newfunction {
[spinner stopAnimating];
}
Thanks.
Just see that the "//parse data from internet " is synchronous or asynchronous. Asynchronous would mean that a separate thread would start from that point on, and the current function execution will continue without delay.
In your second example, you are explicitly making separate thread, which means that #selector(function) will happen on a separate thread, and the next statement [spinner stopAnimating] is executed immediately. So, it seems like spinner is not spinning at all.
Moreover, make sure you start and stop the activity indicator on main thread only.