异步调用目标C iPhone [英] Async call Objective C iphone

查看:123
本文介绍了异步调用目标C iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个网站 - XML获取数据。一切工作正常。

I'm trying to get data from a website- xml. Everything works fine.

但仍然UIButton的,直到pssed将XML数据返回,因此,如果那里有与互联网服务的一个问题,它不能被纠正和应用程序几乎是无法使用。$ P $

But the UIButton remains pressed until the xml data is returned and thus if theres a problem with the internet service, it cant be corrected and the app is virtually unusable.

这里的电话:

{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if(!appDelegate.XMLdataArray.count > 0){
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        [appDelegate GetApps]; //function that retrieves data from Website and puts into the array - XMLdataArray.

    }
    XMLViewController *controller = [[XMLViewController alloc] initWithNibName:@"MedGearsApps" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}

它工作得很好,但我怎样才能使视图按钮功能让滞留。换句话说,我只是想的UIButton和其他UIButtons是功能田地的东西在后台工作。

It works fine, but how can I make the view buttons functional with getting stuck. In other words, I just want the UIButton and other UIButtons to be functional whiles the thing works in the background.

我听说performSelectorInMainThread,但我不能把它正确地练习

I heard about performSelectorInMainThread but i cant put it to practice correctly

任何帮助是AP preciated:)

any help is appreciated :)

推荐答案

您没有太多了解的线程模型,你可能要去搬起石头砸自己的脚,如果你开始增加异步code没有真正理解这是怎么回事。

You don’t understand the threading model much and you’re probably going to shoot yourself in the foot if you start adding asynchronous code without really understanding what’s going on.

在code您在主应用程序线程中写道运行。但是当你仔细想想,你不用写任何功能 - 你只需要实现应用程序委托和事件回调(如触摸处理)和不知何故,他们在时机成熟时自动运行。这不是魔术,这是一个简单的可可对象叫做<一个href=\"http://developer.apple.com/mac/library/documentation/cocoa/conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html\">Run循环。

The code you wrote runs in the main application thread. But when you think about it, you don’t have to write no main function — you just implement the application delegate and the event callbacks (such as touch handlers) and somehow they run automatically when the time comes. This is not a magic, this is simply a Cocoa object called a Run Loop.

运行循环是接收所有事件的对象,处理定时器(如的NSTimer ),并运行于code。这意味着,当你,例如,做一些事情,当用户点击一个按钮,调用树看起来有点像这样:

Run Loop is an object that receives all events, processes timers (as in NSTimer) and runs your code. Which means that when you, for example, do something when the user taps a button, the call tree looks a bit like this:

main thread running
    main run loop
        // fire timers
        // receive events — aha, here we have an event, let’s call the handler
        view::touchesBegan…
            // use tapped some button, let’s fire the callback
            someButton::touchUpInside
                yourCode

现在您code 做你想要做什么和运行循环继续运行。但是当你的code需要很长时间才能完成,比如在你的情况下,运行的循环必须等待,因此事件不会得到处理,直到你的code饰面。这是你在​​你的应用程序中看到。

Now yourCode does what you want to do and the Run Loop continues running. But when your code takes too long to finish, such as in your case, the Run Loop has to wait and therefore the events will not get processed until your code finishes. This is what you see in your application.

要解决,你必须在另一个线程运行长时间操作的情况。这是不是很辛苦,但你必须要想到不过了一些潜在的问题。运行在另一个线程可以那么容易,因为调用<一个href=\"http://developer.apple.com/mac/library/documentation/cocoa/reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/performSelectorInBackground%3awithObject%3a\"><$c$c>performSelectorInBackground:

To solve the situation you have to run the long operation in another thread. This is not very hard, but you’ll have to think of a few potential problems nevertheless. Running in another thread can be as easy as calling performSelectorInBackground:

[appDelegate performSelectorInBackground:@selector(GetApps) withObject:nil];

和现在你必须想办法告诉数据已加载的应用程序,如使用的通知或调用在主线程的选择。顺便说一句:存储应用程序委托数据(甚至使用加载数据的应用程序委托)是不是很优雅的解决方案,但这是另一个故事

And now you have to think of a way to tell the application the data has been loaded, such as using a notification or calling a selector on the main thread. By the way: storing the data in the application delegate (or even using the application delegate for loading the data) is not very elegant solution, but that’s another story.

如果你选择了 performSelectorInBackground 解决方案,看看如何一个相关的问题在辅助线程内存管理的。你需要自己的自动释放池,这样你就不会泄漏自动释放的对象。

If you do choose the performSelectorInBackground solution, take a look at a related question about memory management in secondary threads. You’ll need your own autorelease pool so that you won’t leak autoreleased objects.

更新一段时间后的答案 - 现在它通常是最好的使用运行在后台的code大中央调度:

Updating the answer after some time – nowadays it’s usually best to run the code in background using Grand Central Dispatch:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // No explicit autorelease pool needed here.
    // The code runs in background, not strangling
    // the main run loop.
    [self doSomeLongOperation];
    dispatch_sync(dispatch_get_main_queue(), ^{
        // This will be called on the main thread, so that
        // you can update the UI, for example.
        [self longOperationDone];
    });
});

这篇关于异步调用目标C iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆