多线程iOS,快速启动UI

时间:2016-10-28 07:04:12

标签: ios objective-c xcode multithreading

在我的ios应用程序中,我已经包含谷歌,脸书和推特集成。应用程序启动时,它会在加载UI之前加载API。

如何多线程作为我的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;
}

1 个答案:

答案 0 :(得分: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将获得通知。