简体   繁体   English

NSFileHandle和编写Asynch到iOS中的文件

[英]NSFileHandle & Writing Asynch to a file in iOS

I have a situation that I receive a byte data through Web Services request and want to write it to a file on my iOS device. 我有一种情况,我通过Web服务请求收到字节数据,并希望将其写入我的iOS设备上的文件。 I used to append all data (till end of data) in a memory variable and at the end writing the data using NSStream to a file in my iOS device using method: 我曾经将所有数据(直到数据末尾)附加到内存变量中,最后使用NSStream将数据写入我的iOS设备中的文件:

stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent

It works fine for small size of data, but the problem is if I am receiving data via web services it could be a big chunk (couple MBs) and I don't want to collect all in memory to write it to the file, to make it efficent I think I have to switch to NSFileHandle to write data in a small chunk size to the same file in several times. 它适用于小型数据,但问题是,如果我通过Web服务接收数据,它可能是一个很大的块(几个MB),我不想收集所有内存写入文件,到让它高效我想我必须切换到NSFileHandle ,将数据块的小块大小写入同一个文件几次。 Now my question is what is the best approach to do this? 现在我的问题是这样做的最佳方法是什么? I mean how can I do write to the file in BACKGROUND using NSFileHandle ? 我的意思是如何使用NSFileHandle在BACKGROUND中写入文件? I use code like this: 我使用这样的代码:

 - (void) setUpAsynchronousContentSave:(NSData *) data {

       NSString *newFilePath = [NSHomeDirectory()  stringByAppendingPathComponent:@"/Documents/MyFile.xml"];
     if(![[NSFileManager defaultManager] fileExistsAtPath:newFilePath ]) {
         [[NSFileManager defaultManager] createFileAtPath:newFilePath contents:nil attributes:nil];
     }

     if(!fileHandle_writer) {
         fileHandle_writer = [NSFileHandle fileHandleForWritingAtPath:newFilePath];
     }
     [fileHandle_writer seekToEndOfFile];
     [fileHandle_writer writeData:data];

} }

but with passing a data size of 1-2 Mb to above method, do I need to make it running in background? 但是通过将1-2 Mb的数据大小传递给上面的方法,我是否需要让它在后台运行? FYI I'm writing in main thread. 仅供参考我在主线上写作。

Maybe you can try Grand Central Dispatch . 也许你可以尝试Grand Central Dispatch

I spent some time trying it, bellow is my way to do it. 我花了一些时间尝试它,下面是我的方式去做。

According to Apple's document , if our program need executing only one task at a time, we should create a "Serial Dispatch Queue".So, first declare a queue as iVar. 根据Apple的文档 ,如果我们的程序一次只需执行一个任务,我们应该创建一个“Serial Dispatch Queue”。因此,首先将队列声明为iVar。

dispatch_queue_t queue;

create a serial dispatch queue in init or ViewDidLoad using 使用在initViewDidLoad创建一个串行调度队列

if(!queue)
{
    queue = dispatch_queue_create("yourOwnQueueName", NULL);
}

When data occurs, call your method. 发生数据时,请调用您的方法。

- (void) setUpAsynchronousContentSave:(NSData *) data { 

    NSString *newFilePath = [NSHomeDirectory()  stringByAppendingPathComponent:@"/Documents/MyFile.xml"];
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    if(![fileManager fileExistsAtPath:newFilePath ]) {
        [fileManager createFileAtPath:newFilePath contents:nil attributes:nil];
    }

    if(!fileHandle_writer) {
        self.fileHandle_writer = [NSFileHandle fileHandleForWritingAtPath:newFilePath];
    }
    dispatch_async( queue ,
                   ^ {
                       // execute asynchronously
                       [fileHandle_writer seekToEndOfFile];
                       [fileHandle_writer writeData:data];
                   }); 
}

At last, we need to release the queue in ViewDidUnload or dealloc 最后,我们需要在ViewDidUnloaddealloc释放队列

if(queue)
{
    dispatch_release(queue);
}

I combine these code with ASIHttp, and it works. 我将这些代码与ASIHttp结合起来,并且它可以工作。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM