2016-10-28 13 views
-1

私のiosアプリには、Google、Facebook、Twitterの統合が含まれています。アプリケーションが起動すると、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 conceptを使用できます。

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は通知を受け取ります。