2013-03-27 101 views
0
-(void)application :(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 

    [self addMessageFromRemoteNotification:userInfo updateUI:NO]; 

} 

我需要在performSelectorInBackground方法中運行上面的方法。但是有單個對象的選項。如何更改我的代碼?用performSelector方法傳遞兩個參數

+0

將需要發送的對象包裝到數組中。 :) – Luke 2013-03-27 11:08:52

+0

我該怎麼做? – user2134883 2013-03-27 11:13:38

+1

我相信這是你正在尋找作爲參考: http://stackoverflow.com/questions/8439052/ios-how-to-implement-a-performselector-with-multiple-arguments-and-with -afterd – 2013-03-27 11:15:36

回答

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立即返回,然後該塊將在後臺異步執行。

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]]; 
} 
+0

[self performSelectorInBackground:@selector(backgroundMethod :) withObject:@ {@「userInfo」:userInfo,@「updateUI」:@(NO)}]; – user2134883 2013-03-28 06:08:00

+0

在程序中出現意外的錯誤 – user2134883 2013-03-28 06:08:43