-1

I am new to iOS and I need some feedback about how I can display the output on Iphone screen in an infinite loop.

When the button is pressed, the application goes in an endless loop and creates some outputs. I would like to display these outputs on Iphone screen. Although I get those values from printf on debug screen, nothing shows on Iphone screen. If I would try this function without the infinite loop, I would be able to see the output on screen, however the application must run in infinite loop.

Since I am quite new to programming, does the application need to be run in multi-thread mode?

The code is below:

-(IBAction)button:(UIButton *)sender{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        int output = -1;

        while (pool) {

            //output is received

            if((output != -1) ) {

                printf("%i \n", output); // the output is seen on debug screen

                self.display.text = [NSString stringWithFormat:@"%i",output]; // the outputs is not shown on iphone

                //[pool release];

            }
        }
    }
Sumit Mundra
  • 3,891
  • 16
  • 29
  • Yes, you need to run the loop on another thread, and post the results to the UI thread to show on the screen. There's a Threading Programming Guide (https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html) on the documentation that might be helpful – Marcelo Apr 09 '13 at 13:42

2 Answers2

1

You're not changing the value of output, so if it enter the loop it will never change.

The app is already multi-threaded, it cannot be otherwise - you're probably talking about background tasks (http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

Whether you should be on a different thread depends on how controlled the loop is as, any heavy work, or large large numbers should be taken off of the main thread with something similar to:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //My background stuff

    dispatch_async(dispatch_get_main_queue(), ^{
        //My ui code
    });
});

Regarding threading, GCD is quite simple to use: (https://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html) - there are plenty of tutorials around for GCD

user352891
  • 1,181
  • 1
  • 8
  • 14
0

I think issue is fast changing text not let us see output.

Try this and tell me know what you are getting.

self.display.text = [NSString stringWithFormat:@"%@%i",self.display.text, output];
CRDave
  • 9,279
  • 5
  • 41
  • 59