-2

嗨朋友,我是 Objective-ci 的初学者,由于同步调用,服务器端的响应很慢。我在 google 中分析,调用可能是异步的,意味着响应速度会很高,但我对NSURLConnectionand了解不多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];
}
  }`
4

2 回答 2

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];
        });
}
于 2013-03-26T08:33:28.007 回答
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

于 2013-03-26T08:36:27.543 回答