-2

こんにちは友人私は客観的なciの初心者で、同期呼び出しのためにサーバー側からの応答が遅いです。Google で分析したところ、呼び出しが非同期である可能性があるということは、応答速度が速くなることを意味しますが、 と についてはよくわかりませNSURLConnectionGCD。私の呼び出しを非同期に変更する方法を教えてください。以下の私のコードを参照してください`

- (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 に答える