I'm experiencing an access problem in my Cordova plugin: my NSFileHandle
"loses" context between Cordova calls, and I end up with either an EXC_BAD_ACCESS, a SIGABRT or an Unrecognized Selector sent to instance error. Debugging and digging inside Obj-C's documentation has given me no lead on this, so I'd appreciate your help here very much!
我在Cordova插件中遇到访问问题:我的NSFileHandle在Cordova调用之间“失去”上下文,最后我发送了一个EXC_BAD_ACCESS,一个SIGABRT或一个无法识别的Selector发送到实例错误。在Obj-C的文档中进行调试和挖掘使我无法做到这一点,所以我非常感谢你的帮助!
Here's my code. First - the interface:
这是我的代码。首先 - 界面:
@interface MyPlugin : CDVPlugin
...
- (void) startWriting:(CDVInvokedUrlCommand*)command;
- (void) stopWriting:(CDVInvokedUrlCommand*)command;
@end
And the implementation:
并实施:
....
static NSFileHandle *logFile;
@implementation MyPlugin
- (void) startWriting:(CDVInvokedUrlCommand*)command{
logFile = [NSFileHandle fileHandleForWritingAtPath:@"path_to_my_file"];
NSData nsData = [@"Hello World!" dataUsingEncoding:NSUTF8StringEncoding];
[logFile writeData:nsData];
}
- (void) stopWriting:(CDVInvokedUrlCommand*)command{
NSData nsData = [@"Goodbye World!" dataUsingEncoding:NSUTF8StringEncoding];
[logFile writeData:nsData];
}
I call startWriting
and then stopWriting
using cordova.exec
. The error occurs on the last line of stopWriting
. There were a few times that the problem miraculously disappeared, but in most cases I get one of the aforementioned errors.
It appears that my logFile
object closes the file seamlessly, but according to iOS documentation, this usually happens when the NSFileHandle
object is deallocated, whereas my object is declared as static, and is not supposed to be deallocated as long as my plugin lives (plus, I see in the XCode debugger that it is still allocated).
我调用startWriting然后使用cordova.exec stopWriting。该错误发生在stopWriting的最后一行。有几次这个问题奇迹般地消失了,但在大多数情况下,我得到了上述错误之一。看来我的logFile对象无缝地关闭了文件,但根据iOS文档,这通常发生在NSFileHandle对象被释放时,而我的对象被声明为静态,并且只要我的插件存在就不应该释放(加上,我在XCode调试器中看到它仍然被分配)。
What, in your opinion, causes my NSFileHandle
object to "lose" the actual file?
在您看来,什么导致我的NSFileHandle对象“丢失”实际文件?
1 个解决方案
#1
1
Imho - logFile is released once function finishes its job. You should change your code to something like
Imho - logFile在函数完成其工作后发布。您应该将代码更改为类似的内容
if (logFile==nil) logFile = [NSFileHandle fileHandleForWritingAtPath:@"path_to_my_file"];
or manually retain/release the logFile object.
或手动保留/释放logFile对象。
#1
1
Imho - logFile is released once function finishes its job. You should change your code to something like
Imho - logFile在函数完成其工作后发布。您应该将代码更改为类似的内容
if (logFile==nil) logFile = [NSFileHandle fileHandleForWritingAtPath:@"path_to_my_file"];
or manually retain/release the logFile object.
或手动保留/释放logFile对象。