本文介绍了在主线程上处理NSFileHandle上的writeData方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于writeData调用是同步的,所以当我们从主队列以外的其他线程调用writeData时,使用它的最佳方法是什么?

Since writeData call is synchronous, what is the best way to use it when we call writeData from a different thread other than main queue?

例如,调用Web服务以获取一些数据,并将完成处理程序分配给Web服务调用.现在,此完成处理程序将在其他线程上执行(不在主队列上执行).

For instance, a web service is called to fetch some data and the completionHandler is assigned to the web service call. Now this completion handler will be executed on a different thread (not on main queue).

我已经看到我的应用在writeData方法上卡住了5至6分钟.这是我现在唯一可以怀疑的东西.

I have seen my app getting stuck, on writeData method for 5 to 6 mins. This is the only thing I can suspect right now.

我尝试用dispatch_async(mainQueue)包裹writeData调用,但是没有用.

I tried wrapping around my writeData call with dispatch_async(mainQueue) but it did not work.

- (void) writeToFile: (NSString *) targetString
{
    //_loggingString holds the data, which keeps on accumulating as the user performs operations. At some point of time (callbacks from API's I call this method, to actually, write this string in the file and clear this string afterwards.)
    NSString *oldString = [_loggingString copy];

    _loggingString = [oldString stringByAppendingString:targetString];

    if (![[NSFileManager defaultManager]fileExistsAtPath:@"somePath"])
    {
        [[NSFileManager defaultManager]createFileAtPath:@"somePath" contents:nil attributes:nil];
    }

    NSFileHandle *fileHandle =  [NSFileHandle fileHandleForWritingAtPath:@"somePath"];

    [fileHandle seekToEndOfFile];

    [fileHandle writeData:[_loggingString dataUsingEncoding:NSUTF8StringEncoding]];

    _loggingString = @"";
}

推荐答案

不建议在主线程中执行文件写入操作.另外,默认的全局队列会导致性能出现问题,因为系统无法确定任务的优先级.

It is not preferred to do file write operation in main thread. Also, there will be issue in performance with default global queue, as the system cannot prioritize the task.

因此,尝试创建4种类型的后台队列:

So try to create 4 types of background queues:

dispatch_queue_t GlobalUserInteractiveQueue(void) {
    return dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0);
}

dispatch_queue_t GlobalUserInitiatedQueue(void) {
    return dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
}

dispatch_queue_t GlobalUtilityQueue(void) {
    return dispatch_get_global_queue(QOS_CLASS_UTILITY, 0);
}

dispatch_queue_t GlobalBackgroundQueue(void) {
    return dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0);
}

在您的代码中执行以下操作:

In your code just do this:

创建自定义队列.queue = dispatch_queue_create("customQueueName", NULL);

create custom queue.queue = dispatch_queue_create("customQueueName", NULL);

然后在分派异步中编写代码

then write code in dispatch async

dispatch_async( queue ,
                   ^ {
                       // execute asynchronously
                       [fileHandle seekToEndOfFile];
                       [fileHandle writeData:[_loggingString dataUsingEncoding:NSUTF8StringEncoding]];

                   });

在这里检查每个队列的工作过程: https://gist.github.com/ankitthakur/dd945a66924fbd697169

Check the working process of each queue here:https://gist.github.com/ankitthakur/dd945a66924fbd697169

这篇关于在主线程上处理NSFileHandle上的writeData方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 13:22