NSXMLParser 和视图生命周期中的多次调用。
目前,我在使用 -(void)viewDidLoad
中的两个函数时遇到问题,这两个函数都使用 NSUrlRequest
将 HTTPPost
发送到网络服务接收数据。
它工作正常,直到 [self bar]
决定在 [self foo]
完全完成之前启动。那么,有没有什么聪明的方法可以在开始 [self foo] 之前检查 [self bar]
是否完全完成?
-(void)viewDidLoad{
[self foo]; // initiates a nsxmlparsercall to a webservice to get values.
[self bar]; // relies on the values recieved from [self foo] to make it's own call.
/* However, [self bar] always crashes before initiating it's request.
/* It crashes when the variables that are being sent with the poststring
/* are being set, as they are null.
/* Which means that the `[self foo]` doesnt get completed before starting [self bar];
}
在这一点上我可能会非常不高兴,我什至考虑过重写 -(void)viewDidload
并设置一个 bool 来控制何时可以触发第二个函数,但这似乎是超级糟糕的编码。 任何关于
如何为我指明正确方向的建议和/或提示将受到高度赞赏。提前致谢。
Currently i'm having trouble using two functions in my -(void)viewDidLoad
, both of these functions uses NSUrlRequest
to send HTTPPost
to a webservice to recieve data.
It works fine untill [self bar]
decides to kick in before [self foo]
is completely finished. So, is there any smart way of checking if [self bar]
is completely finished before starting [self foo]?
-(void)viewDidLoad{
[self foo]; // initiates a nsxmlparsercall to a webservice to get values.
[self bar]; // relies on the values recieved from [self foo] to make it's own call.
/* However, [self bar] always crashes before initiating it's request.
/* It crashes when the variables that are being sent with the poststring
/* are being set, as they are null.
/* Which means that the `[self foo]` doesnt get completed before starting [self bar];
}
I might be very off at this point, i've even considered overriding -(void)viewDidload
and setting a bool to control when it's ok to fire the second function, but that seems like super poor coding..
Any suggestions and/or tips on how to point me in the right direction will be highly appreciated. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我放置函数的最佳位置将是 nsxmlparser 的委托方法之一,
如果您在后台线程上解析响应并且调用函数栏并不重要,那么这很好主线程或后台线程。
但是如果你想在主线程上专门调用 bar 函数那么你可以使用这个函数
I best place to put your function will be one of the delegate methods of
nsxmlparser
that isThis fine if you are parsing the response on a background thread and it doesn't matter if the function bar is called on main thread or background thread.
But if you want to call the bar function specifically on main thread then you can use this function
你的意思是在 [self foo] 函数中你想解析一些东西,当它完全解析后你想调用 [self bar];功能对吗?
好的,那么您可以在解析完成时发出通知。通过此通知您可以调用您想要的方法。
you mean in [self foo] function you want to parse some thing and when its completely parsed then you want to call [self bar]; function right?
okay then you can fire a notification when parsing gets completed. in by this notification you can call the method you want.