zoukankan      html  css  js  c++  java
  • dispatch_async 线程分发注意事项. 不及格的程序员

     1 // NOTE:  GameCenter does not guarantee that callback blocks will be execute on the main thread.
     2 // As such, your application needs to be very careful in how it handles references to view
     3 // controllers.  If a view controller is referenced in a block that executes on a secondary queue,
     4 // that view controller may be released (and dealloc'd) outside the main queue.  This is true
     5 // even if the actual block is scheduled on the main thread.  In concrete terms, this code
     6 // snippet is not safe, even though viewController is dispatching to the main queue:
     7 //
     8 //    [object doSomethingWithCallback:  ^()
     9 //    {
    10 //        dispatch_async(dispatch_get_main_queue(), ^(void)
    11 //        {
    12 //            [viewController doSomething];
    13 //        });
    14 //    }];
    15 //
    16 // UIKit view controllers should only be accessed on the main thread, so the snippet above may
    17 // lead to subtle and hard to trace bugs.  Many solutions to this problem exist.  In this sample,
    18 // I'm bottlenecking everything through  "callDelegateOnMainThread" which calls "callDelegate".
    19 // Because "callDelegate" is the only method to access the delegate, I can ensure that delegate
    20 // is not visible in any of my block callbacks.
    21 
    22 - (void) callDelegate: (SEL) selector withArg: (id) arg error: (NSError*) err
    23 {
    24     assert([NSThread isMainThread]);
    25     if([delegate respondsToSelector: selector])
    26     {
    27         if(arg != NULL)
    28         {
    29             [delegate performSelector: selector withObject: arg withObject: err];
    30         }
    31         else
    32         {
    33             [delegate performSelector: selector withObject: err];
    34         }
    35     }
    36     else
    37     {
    38         NSLog(@"Missed Method");
    39     }
    40 }
    41 
    42 - (void) callDelegateOnMainThread: (SEL) selector withArg: (id) arg error: (NSError*) err
    43 {
    44     dispatch_async(dispatch_get_main_queue(), ^(void)
    45     {
    46        [self callDelegate: selector withArg: arg error: err];
    47     });
    48 }
    49 
    50 - (void) authenticateLocalUser
    51 {
    52     if([GKLocalPlayer localPlayer].authenticated == NO)
    53     {
    54         [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error)
    55         {
    56             [self callDelegateOnMainThread: @selector(processGameCenterAuth:) withArg: NULL error: error];
    57         }];
    58     }
    59 }
  • 相关阅读:
    C# 注册Dll文件
    WPF强制设置TextBox文本框的焦点
    WPF中MVVM模式下控件自有的事件绑定
    第2章 数字之魅——数字中的技巧2.8
    具体数学斯特林数-----致敬Kunth
    一个数的约数(个数。约数和)
    hdu 1796 How many integers can you find 容斥定理
    读贾志鹏线性筛有感 (莫比乌斯函数的应用)
    欧拉函数小结
    莫比乌斯函数
  • 原文地址:https://www.cnblogs.com/ioriwellings/p/2441785.html
Copyright © 2011-2022 走看看