2017-02-22 1 views
0

Ich habe eine Methode, die Methode habe den Nsdata-Wert zurückgegeben, aber ich weiß nicht, wie der Rückgabewert von NSURLSessionDataTask Block erhalten. und wie die getDownloadFileData methods.Code für Aufgabe nennen ist: -Wie bekomme ich den Rückgabewert von NSURLSessionDataTask und Call Block?

Anrufer:

NSData *getFileDataResult = [self getDownloadFileData:pathString]; 

Methode:

- (NSData*) getDownloadFileData : (NSString*) filePath { 

    NSURLSessionDataTask *downloadFile = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:filePath] completionHandler:^(NSData *fileData, NSURLResponse *response, NSError *error){ 
// ..... 
// fileData should return out. 

    [downloadFile resume]; 
    }); 
    // I want to return the fileData after download completion. 
    // how to return? 
} 

jemand kann mir eine Hand?

Vielen Dank.

+0

können Sie Abschlussblöcke verwenden, schauen Sie sich http://StackOverflow.com/Questions/21436831/How-to-Write-an-Objective-c-Completion-Block – raki

Antwort

0

Zunächst einmal haben Sie die Lebenslauf-Methode an die falsche Stelle gesetzt. Es sollte so aussehen:

- (NSData*) getDownloadFileData : (NSString*) filePath { 
    NSURLSessionDataTask *downloadFile = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:filePath] completionHandler:^(NSData *fileData, NSURLResponse *response, NSError *error){ 
    // ..... 
    // fileData should return out. 


     }); 
[downloadFile resume];//NOTICE THE PLACEMENT HERE 
     // I want to return the fileData after download completion. 
     // how to return? 
    } 

Die zweite Sache ist, können Sie einfach eine NSData Variable erstellen und den Wert im Abschluss block eher zuweisen als vorbei data zurück.

ODER

tun einfach so in Fertigstellung Block

if(fileData){ 
    return fileData; 
} 
0

Bitte meine Antwort überprüfen, ich hoffe, dies hilfreich

- (NSData *)getDownloadFileData:(NSString *)filePath { 
    __block NSData *responseData = nil; 

    dispatch_semaphore_t sema = dispatch_semaphore_create(0); 
    NSURLSessionDataTask *downloadFile = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:filePath] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
     responseData = data; 
     dispatch_semaphore_signal(sema); 
    }]; 
    [downloadFile resume]; 

    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); 

    return responseData; 
} 

- (void)whereToCall { 
    // Because to prevent the semaphore blocks main thread 
    dispatch_queue_t myQueue = dispatch_queue_create("com.abc", 0); 
    dispatch_async(myQueue, ^{ 
     NSData *data = [self getDownloadFileData:@"urlString"]; 
    }); 
} 

- (void)betterGetDownloadFileData:(NSString *)filePath completion:(void (^)(NSData * __nullable data))completion { 
    NSURLSessionDataTask *downloadFile = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:filePath] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
     if (completion) { 
      completion(data); 
     } 
    }]; 
    [downloadFile resume]; 
} 

Ich empfehle, sollten Sie Ihren Code entwerfen als meine schlagen vor, stattdessen block zu verwenden.

Verwandte Themen