GCD dispatch_sync没有按顺序完成执行

时间:2014-06-18 21:07:02

标签: ios objective-c

我有一个方法可以帮助我将本地standardUserDefaults与Web服务同步。首先,我需要确保数据已成功同步,然后我可以让方法返回。我目前正在努力解决的问题是,我无法将GCD变为执行&按特定顺序完成任务:首先发布到网络服务,其次返回。

问题是第二个dispatch_sync任务首先完成执行,然后是第一个dispatch_sync任务。如何确保按计划执行?这是一个特例,因为NSURLSessionDataTask?

非常感谢!

-(void) syncLocalStorageToMMUsersDB:(void(^)(bool success))handler
{
__block bool fb_sync_success = 0;

dispatch_queue_t requestQueue = dispatch_queue_create("com.micromorts.request", DISPATCH_QUEUE_SERIAL);


//if logged in with Facebook, then sync it
if ([_defaults stringForKey:MMFBIdPrefsKey]) {
  // Start NSURLSession
  NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
  NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

  // POST parameters
  NSURL *url = [NSURL URLWithString:@"http://THE.URL.GOES.HERE"];
  NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
  NSString *params = [POST PARAMS GO HERE];
  [urlRequest setHTTPMethod:@"POST"];
  [urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

  // NSURLSessionDataTask returns data, response, and error

  NSURLSessionDataTask *dataTask =[defaultSession dataTaskWithRequest:urlRequest
    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        // Handle response
        NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
        NSInteger statusCode = [httpResponse statusCode];
        if(error == nil) {
          if (statusCode == 404) {
              NSLog(@"MMAuth.m :: syncLocalStorageToMMUsersDB FB :: FB User not found");
              fb_sync_success = false;

          } else if (statusCode == 200) {
              NSLog(@"MMAuth.m :: syncLocalStorageToMMUsersDB FB :: Successfully synced ");
              fb_sync_success = true;

          } else {
          NSLog(@"MMAuth.m :: syncLocalStorageToMMUsersDB FB :: Sync fail ");
              fb_sync_success = false;
          }
        }
    }
  ];
  //This should finish EXECUTING first
  dispatch_sync(requestQueue, ^{
      [dataTask resume];
  });
}

//This should finish EXECUTING second
dispatch_sync(requestQueue, ^{

    if (fb_sync_success){
       NSLog(@"SUCCESS");
    } else {
       NSLog(@"FAILED");
    }

});
}

1 个答案:

答案 0 :(得分:3)

如果对[dataTask resume];的调用是异步的,那么dispatch_sync()将在-resume执行后立即返回,然后后续的dispatch_sync()将被排队并立即执行(但是在加载数据之前。)

将您的SUCCESS / FAILED逻辑移动到完成处理程序块中;这就是完成处理程序存在的原因。