2016-10-28 116 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将得到通知。