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?
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).
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.
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 = @"";
}