1

I am learning GCD and I am wondering if the following is OK or: is there a better way to do?

I am also wondering if I need to weakify self in what follows. I am using ARC.

id someObject = [self getSomeObject] ;

dispatch_queue_t newThread = dispatch_queue_create("New thread", NULL) ;

dispatch_async(newThread, ^
               {
                   [self doSomeStuff] ;

                   [someObject doSomeStuffOnMyObject] ;

                   /*
                    Back on the main thread
                    */
                   dispatch_async(dispatch_get_main_queue(), ^
                                  {
                                      [self doSomeStuffMore] ;

                                      [someObject doSomeStuffOnMyObjectMore] ;
                                  }) ;
               }) ;

So, if this is the good way to do, should I create a category over NSObject?

Colas
  • 3,473
  • 4
  • 29
  • 68

1 Answers1

2

The most common practice is what you have:

dispatch_async(dispatch_get_global_queue(priority, flags), ^(void) {
    // code

    dispatch_async(dispatch_get_main_queue(), ^(void) {
        // code
    });
});

If you plan on modifying someObject within the block and using it afterward then you should prefix it with __block - more information here

__block id someObject = [self getSomeObject];

P.S. your semicolon style is unique :D

Community
  • 1
  • 1
JuJoDi
  • 14,627
  • 23
  • 80
  • 126
  • Thanks! Happy to do it well! My semi-colon style is the French style! (we put a space before ";")... – Colas Feb 28 '14 at 18:59