嗨朋友,我是 Objective-ci 的初学者,由于同步调用,服务器端的响应很慢。我在 google 中分析,调用可能是异步的,意味着响应速度会很高,但我对NSURLConnection
and了解不多GCD
。所以请帮助我如何改变我的异步调用。请参阅下面的代码`
- (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];
}
}`