0

Please consider following code:

- (void)viewDidLoad {
    [super viewDidLoad];

    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{


        for (int i; i < 100 ; i++) {

            NSLog(@"%i", i);

        }

    });

    NSLog(@"Main thread code");

}

Why its only output is - 2016-01-26 16:15:28.842 Test[1051:35933] Main thread code ?

I mean, code in global queue is never executed, why? I only want it to execute synchronously. Of course i could easy write dispatch_async, but im just curious why code above not work, does not output anything execept 2016-01-26 16:15:28.842 Test[1051:35933] Main thread code?

8
  • 2
    works fine for me... I think your problem is elsewhere Commented Jan 26, 2016 at 13:25
  • @originaluser2 i paste that code in empty project, i created for testing purpose. Commented Jan 26, 2016 at 13:27
  • 1
    I also pasted the code into an empty project, and it worked! Although it's also worth mentioning that this code makes no sense. This will block the main thread until something on a background thread has completed... why not just run it on the main thread in that case? You should be using dispatch_async for this. Commented Jan 26, 2016 at 13:28
  • 1
    no, I mean it blocks the main thread while it is running. Once it's finished running, the main thread will continue. Also, try using %d as your string formatting option. Not sure what %i is. Commented Jan 26, 2016 at 13:30
  • 3
    problem is in for (int i; i < 100 ; i++) u r not initialise value of i , for (int i = 0 ; i < 100 ; i++) Commented Jan 26, 2016 at 13:42

1 Answer 1

1

problem is in

for (int i; i < 100 ; i++) 

for above code value of i = 72339625 so condition not satisfied and not goes into loop

solution : initialise value of i ,

for (int i = 0 ; i < 100 ; i++)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.