Krzemienski
Krzemienski

Reputation: 1185

writeToFile how to tell when it is completed

I'm doing some writing of data to my documents directory and I was wondering if there is a way to tell when my writeToFile method completes and the file I am creating has be fully created. Thanks in advance here is the method I am calling I am just looking for a way wether it is a delegate method or some other way to tell when this completes.

[imageData writeToFile:fullPathToFile atomically:YES];

Nick

Upvotes: 20

Views: 8693

Answers (2)

atereshkov
atereshkov

Reputation: 4555

Swift 5:

do {
    try data.write(to: path)
    // here's when write operation is completed without errors thrown
} catch {
    Swift.print(error)
}

Upvotes: 7

sch
sch

Reputation: 27506

The method writeToFile:atomically is "synchronous". It will write the file and then return YES or NO depending on wether the file was successfully written or not.

That means that the operation is complete as soon as the method returns.

BOOL success = [imageData writeToFile:fullPathToFile atomically:YES];
// Now, the operation is complete
// success indicates whether the file was successfully written or not

Upvotes: 34

Related Questions