有没有办法让FSNConnection保持同步?

时间:2013-02-22 11:00:11

标签: objective-c xcode4.5

NSString *mediaType = info [UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
    NSURL * URL = [NSURL URLWithString:[BGMakeURL URLForUploadImage]];
    UIImage * image = info[UIImagePickerControllerEditedImage];
    if(!image)
    {
        image= info[UIImagePickerControllerOriginalImage];
    }
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];

    parameters [@"__VIEWSTATE"]=@"/wEPDwUKMTQxNzIxMTc0MQ9kFgICAw8WAh4HZW5jdHlwZQUTbXVsdGlwYXJ0L2Zvcm0tZGF0YWRkpPnRKFYg9pwczRd4Lr3SFyeNR3bJtDpNqNyMm9qs/4g=";
    parameters [@"__EVENTVALIDATION"]=@"/wEWBgLE38oCAofc/4YFAuv/gd4CApahmrIJAuKeq6sMAu3ehcwDnwyRwRhgxmSADdcUMCfAUEl1cW8dliatCxOiAEYU8Jc=";
    parameters [@"Image"]=[FSNData withImage:image jpegQuality:.9 fileName:@"fileName"];




  FSNConnection *connection =
    [FSNConnection withUrl:URL
                    method:FSNRequestMethodPOST
                   headers:nil
                parameters:parameters
                parseBlock:nil
           completionBlock:^(FSNConnection * connection){
               id whatistheresult=connection.parseResult;
               PO(whatistheresult);
               [BGLoadingView toast:@"Image Uploaded"];
           }
             progressBlock:nil];
    [connection start];

就我而言,这不是同步的。如果我想让它同步,那该怎么办?

1 个答案:

答案 0 :(得分:1)

当然,但正确的方法取决于FSNetworking是否使用FSN_QUEUED_CONNECTIONS进行编译(从GitHub上的当前来源判断)。假设它是,那么你会做这样的事情:

dispatch_semaphore_t sem = dispatch_semaphore_create(0);
FSNConnection *connection =
    [FSNConnection withUrl:URL
                    method:FSNRequestMethodPOST
                   headers:nil
                parameters:parameters
                parseBlock:nil
           completionBlock:^(FSNConnection * connection){
               dispatch_semaphore_signal(sem);
           }
             progressBlock:nil];
[connection start];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); // This will block until completion block is called
dispatch_release(sem);

如果没有使用FSN_QUEUED_CONNECTIONS进行编译,则需要旋转运行循环,如下所示:

FSNConnection *connection =
    [FSNConnection withUrl:URL
                    method:FSNRequestMethodPOST
                   headers:nil
                parameters:parameters
                parseBlock:nil
           completionBlock:nil
             progressBlock:nil];
[connection start];

while (!connection.didComplete)
{
    CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, YES);
}

// Execution won't get here until the connection is complete.