NSXMLParser 解析第一次调用委托失败...但在 iPhone 应用程序任务重新启动后可以工作
我有这个奇怪的问题。我正在使用 NSXMLParser 来解析获取的 XML 数据 来自网络。即使“解析”调用返回成功,委托也会起作用 当 iPhone 应用程序第一次运行时不会被调用。但是,一模一样 当我重新启动时,XML 可以通过正确调用的委托回调进行解析。 通过杀死后台任务和进程(运行iOS4.2的iPhone4)重新启动应用程序。 模拟器和手机中的行为完全相同。
NSXMLParser *lxmlParser = [[NSXMLParser alloc] initWithData:jData];
MyXMLParser *pxmlParser = [MyXMLParser initXMLParser];
[lxmlParser setDelegate:pxmlParser];
BOOL success = [lxmlParser parse];
如果我只是将应用程序推到后台并带来 它回到前台,问题仍然存在。唯一的解决方法是杀死正在运行的 后台任务(是的,我的后台任务监听“重大位置变化”) 并重新启动应用程序。从现在开始,解析工作正常......
有人可以帮忙吗?
I have this strange problem. am using NSXMLParser to parse my XML data that is fetched
from the network. Even though the "parse" call returns success, the delegate functions
are not being invoked when the iPhone app runs for the first time. But, the exact same
XML is parsed just fine with the delegate callbacks called properly when I restart the
process (iPhone4 running iOS4.2) by killing the background task & restarting the app.
Exact same behavior in both simulator and phone.
NSXMLParser *lxmlParser = [[NSXMLParser alloc] initWithData:jData];
MyXMLParser *pxmlParser = [MyXMLParser initXMLParser];
[lxmlParser setDelegate:pxmlParser];
BOOL success = [lxmlParser parse];
Instead of killing the background task, if I just push the app to the background and bring
it back to foreground, the problem stays. The only workaround is to KILL the running
background task (yes, mine is a background task listening to "significant location change")
and restarting the app. From now on, the parsing just works fine...
Can someone pls help ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[MyXMLParser initXMLParser]
的定义是什么?如果您遵循 Cocoa 内存管理指南(您应该这样做!),它将返回一个自动释放的对象(因为它不以“alloc”、“new”、“copy”开头)。那么这个对象在哪里永久保留呢?如果不保留它,则当当前自动释放池退出时,它可能会过早地从内存中转储。顺便说一句,我会为你的自动释放构造方法选择一个更好的方法名称——以“init”开头的方法实际上不是 init 方法,这会令人困惑。从风格上来说,将“intXMLParser”重命名为“xmlParser”可能是个好主意。
另外,您已将 XMLParserDelegate 实现类命名为“somethingXMLParser”。更好的选择是“somethingXMLParserDelegate”。
What is the definition of
[MyXMLParser initXMLParser]
? If you're following the Cocoa memory management guideliness (and you should be!), it will return an object that is autoreleased (because it doesn't begin with 'alloc', 'new', 'copy'). So where does this object get retained permanently? If it's not retained, it may be getting dumped prematurely from memory when the current autorelease pool exits.Btw, I'd choose a better method name for your autorelease construction method -- a method beginning with 'init' that isn't actually an init method is confusing. Renaming 'intXMLParser' to simply 'xmlParser' might be a good idea, stylistically.
Also, you've named an XMLParserDelegate implementation class as 'somethingXMLParser'. A better choice would be something like 'somethingXMLParserDelegate'.