2016-10-28 50 views
-1

在我的ios應用程序中,我已經包括谷歌,臉譜和推特整合。當應用程序啓動時,它在加載UI之前加載API。多線程iOS,快速啓動UI

如何以我的UI加載多線程首先快速啓動。我didFinishLaunchingWithOptions是。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     // Add code here to do background processing 
     // 
     // 

     NSLog(@"Thread Excecution started"); 

     NSError* configureError; 
     [[GGLContext sharedInstance] configureWithError: &configureError]; 
     NSAssert(!configureError, @"Error configuring Google services: %@", configureError); 

     [GIDSignIn sharedInstance].delegate = self; 


     [[FBSDKApplicationDelegate sharedInstance] application:application 
           didFinishLaunchingWithOptions:launchOptions]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      // Add code here to update the UI/send notifications based on the 
      // results of the background processing 

      NSLog(@"Thread Excecution completed"); 
     }); 
    }); 
    return YES; 
} 
+0

您知道,即使在主線程中,通知實際上可能會觸發並被接收,但無法保證嗎?你確定要這麼做嗎? – NSNoob

+0

爲什麼你需要在後臺線程中編寫外部API的啓動代碼呢? – NSNoob

+0

我可以在Viewdidload中添加這些代碼嗎?是的,將工作 – Saranjith

回答

2

如果您使用多個線程,那麼您可以使用dispatch_group的概念。

dispatch_group_t group = dispatch_group_create(); 

//block 1 
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{ 
    // code here 
}); 
//block 2 
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{ 
    // code here 
}); 

//block 3 
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{ 
    // block 3 will get notify, after block 1 and block 2 complete their tasks. 
dispatch_async(dispatch_get_main_queue(), ^{ 

     [animationImageView stopAnimating]; 

     [self createUI]; 
    }); 
} 

在這裏,塊1和塊2將平行地運行,並且在他們完成他們的工作之後,塊3將得到通知。