NSXMLParser 和视图生命周期中的多次调用。

发布于 2025-01-01 21:23:26 字数 948 浏览 1 评论 0原文

目前,我在使用 -(void)viewDidLoad 中的两个函数时遇到问题,这两个函数都使用 NSUrlRequestHTTPPost 发送到网络服务接收数据。

它工作正常,直到 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜柠檬 2025-01-08 21:23:26

我放置函数的最佳位置将是 nsxmlparser 的委托方法之一,

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    [self bar];
}

如果您在后台线程上解析响应并且调用函数栏并不重要,那么这很好主线程或后台线程。

但是如果你想在主线程上专门调用 bar 函数那么你可以使用这个函数

[self performSelectorOnMainThread:@SEL(bar) withObject:nil waitUntilDone:YES];

I best place to put your function will be one of the delegate methods of nsxmlparser that is

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    [self bar];
}

This 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 performSelectorOnMainThread:@SEL(bar) withObject:nil waitUntilDone:YES];
晚风撩人 2025-01-08 21:23:26

你的意思是在 [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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文