It is difficult to answer unless you are more specific, but the most effective way to do this is to probably not modify the file at all.
iOS, , . , / , (, iTunes iPhoto do) ?
, C : NSInputStream NSOutputStream, .
, , API:
typedef void (^DataExportHandler)(NSData *resultData, NSError *exportError);
@interface DataStore (FileExport)
- (NSInputStream *)exportStreamForObjectWithIdentifier:(id)identifier error:(NSError * __autoreleasing*)error;
@return A cancellation token.
- (id)asynchronouslyExportDataForObjectWithIdentifier:(id)identifier resultHandler:(DataExportHandler)handler;
- (void)abortAsynchronousExportWithToken:(id)exportToken;
@end
, ARC , , , .
, : rawDataStream, youd , .
, , , Ive , NSStreamDelegate .
, ...
@interface _StreamConsumer : NSObject <NSStreamDelegate> {
NSInputStream *_stream;
DataExportHandler _handler;
NSMutableData *_data;
}
- (id)initWithInputStream:(NSInputStream *)stream resultHandler:(DataExportHandler)handler;
- (void)close;
@end
@implementation DataStore (FileExport)
- (id)asynchronouslyExportDataForObjectWithIdentifier:(id)someUniqueIdentifier resultHandler:(void (^)(NSData *fileData, NSError *exportError))
{
NSParameterAssert(handler);
handler = [handler copy];
NSError *setupError;
NSInputStream *exportStream = [self exportStreamForObjectWithIdentifier:someUniqueIdentifier error:&setupError];
if (!exportStream)
{
dispatch_async(dispatch_get_current_queue(), ^{
handler(nil, setupError);
});
return nil;
}
_StreamConsumer *helper = [[_StreamConsumer alloc] initWithStream:exportStream resultHandler:handler];
return helper;
}
- (void)abortAsynchronousExportWithToken:(id)exportToken
{
[exportToken close];
}
- (NSInputStream *)exportStreamForObjectWithIdentifier:(id)identifier error:(NSError * __autoreleasing*)error
{
NSInputStream *rawDataStream = [NSInputStream inputStreamWithURL:rawFileURL];
if (!rawDataStream)
{
return nil;
}
CFReadStream cfExportStream;
CFWriteStream cfBuffer;
CFStreamCreateBoundPair(kCFAllocatorDefault, &cfExportStream, &cfBuffer, someValueYouHaveTuned);
if (!cfExportStream || !cfBuffer)
{
return nil;
}
NSInputStream *exportStream = (__bridge_transfer NSInputStream *)cfExportStream;
_exportBuffer = (__bridge_transfer NSOutputStream *)cfBuffer;
rawDataStream.delegate = self;
[rawDataStream open];
[rawDataStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunloopDefaultMode];
return exportStream;
}
@end