I am trying to create a file with a unique name and write data to it in the background.
mktemp He speaks Whenever it is possible, mkstemp() should be used instead, since it does not have the race condition.
Usage mkstempleads to an open file descriptor, so dispatch_writeit seems obvious.
Now NSDatashould be enclosed in dispatch_data_tusing dispatch_data_create. Care must be taken of the free memory that must be freed and the storage of the memory that must be retained. In ARC, this is less obvious.
+ (void) createUnique:(NSData*)content name:(NSString*)name
extension:(NSString*)extension
completion:(void (^)(NSURL* url, NSError* error))completion {
dispatch_queue_t queue = dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_data_t data = dispatch_data_create(content.bytes, content.length,
queue, ^{});
dispatch_fd_t descriptor;
strcpy(nameCString, templateCString);
descriptor = mkstemps(nameCString, extensionLength);
free(nameCString);
if (descriptor != -1) {
dispatch_write(descriptor, data, queue,
^(dispatch_data_t data, int error) {
NSData* strongContent = content;
if (error) {
completion(nil, [NSError
errorWithDomain:NSURLErrorDomain
code:error userInfo:nil]);
} else {
completion([NSURL URLWithString:path], nil);
}
});
} else {
completion(nil, [NSError errorWithDomain:NSURLErrorDomain code:errno
userInfo:nil]);
}
}
So the questions are:
- Is it even necessary? Should I be
mktempused with NSData writeToFile:options:error:and not worry about safety / race conditions? dispatch_data_create , ( NSData)?- ,
mkstemps, dispatch_write? NSData dispatch_data_t ? ? ARC?- ?
dispatch_io_close?