-(void)application :(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[self addMessageFromRemoteNotification:userInfo updateUI:NO];
}
我需要在 performSelectorInBackground 方法中运行上述方法。但是有单个对象的选项。如何更改我的代码?
使用 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 立即返回,然后该块将在后台异步执行。
无法通过 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]];
}