使用performSelector方法传递两个参数

时间:2013-03-27 11:08:01

标签: iphone xcode

-(void)application :(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

    [self addMessageFromRemoteNotification:userInfo updateUI:NO];

}

我需要在performSelectorInBackground方法中运行上面的方法。但是有单个对象的选项。如何更改我的代码?

2 个答案:

答案 0 :(得分:2)

使用GCD:

-(void)application :(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self addMessageFromRemoteNotification:userInfo updateUI:NO];
    });

}

dispatch_async立即返回,然后该块将在后台异步执行。

答案 1 :(得分:2)

无法通过performSelectorInBackground传递多个参数。我遇到它时解决问题的方法是通过一个字典,使用文字NSDictionary语法可以更容易。

-(void)application :(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [self performSelectorInBackground:@selector(backgroundMethod:) withObject:@{@"userInfo" : someUserInfoDictionary, @"updateUI" : @(NO)}];
}
-(void)backgroundMethod:(NSDictionary *)params
{
    [self addMessageFromRemoteNotification:params[@"userInfo"] updateUI:[params[@"updateUI"] boolValue]];
}