-2
嗨朋友我是objective-c.i的初學者,由於同步調用,服務器端響應緩慢。我在谷歌分析調用可能是異步意味着響應速度會很高,但我不知道太多關於NSURLConnection
和GCD
。所以請幫助我如何改變我的調用異步。看到我的代碼below`如何在Objective-C中將我的同步更改爲異步調用?
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString* oldToken = [self deviceToken];
NSString *newToken = [[[[deviceToken description]stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
NSLog(@"My token is: %@", newToken);
[self setDeviceToken:newToken];
if (![newToken isEqualToString:oldToken])
{
[self calur:newToken];
}
}
- (NSString*)deviceToken{
return [[NSUserDefaults standardUserDefaults] stringForKey:@"deviceid"];
}
- (void)setDeviceToken:(NSString*)token{
[[NSUserDefaults standardUserDefaults] setObject:token forKey:@"deviceid"];
}
//This function used to store a notification device id to our notification databae
-(void)calur:(NSString *)device
{
NSString *post =[NSString stringWithFormat:@"deviceId=%@",device];
NSString *hostStr = @"https://myserver.com/Ver_2_0/notification/check.php?";
NSError *error = nil;
NSString *nocon=[NSString stringWithContentsOfURL:[NSURL URLWithString:hostStr]encoding:NSUTF8StringEncoding error:&error];
if (nocon == nil)
{
NSLog(@"NO Connection");
}
else
{
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
NSLog(@"hostStr=%@",hostStr);
NSLog(@"serverOutput = %@",serverOutput);
NSLog(@"dataURL=%@",dataURL);
// NSData *dataurl=dataURL;
if([serverOutput isEqualToString:@"Token Updated Successfully"])
{
NSLog(@"badge updated");
}
else
{
NSLog(@"serverOutput = %@",serverOutput);
NSLog(@"not registered");
}
[serverOutput release];
}
}`