-2

嗨朋友我是objective-c.i的初学者,由于同步调用,服务器端响应缓慢。我在谷歌分析调用可能是异步意味着响应速度会很高,但我不知道太多关于NSURLConnectionGCD。所以请帮助我如何改变我的调用异步。看到我的代码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]; 
} 
    }` 

回答

0
if (nocon == nil) 
{ 
    NSLog(@"NO Connection"); 
} 
else 
{ 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
      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]; 
     }); 
} 
0

一个小片段:

#import "AFNetworking.h" 
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:self.value, @"POSTvar", nil]; 
    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL: [NSURL URLWithString:@"http://your.address"]]; 
    NSURLRequest *request = [client requestWithMethod:@"POST" path:nil parameters:params]; 

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
     // do some with JSON 
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
     NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo); 
    }]; 

    [operation start]; 
}); 

AFNetworking github