从另一个线程在主线程上运行方法

时间:2011-02-03 21:56:23

标签: cocoa nsthread

我的模型类必须从互联网上获取一些数据。所以我决定在另一个线程上运行它,所以ui不会冻结。 因此,当一个对象想要一些数据时,它首先使用这种类型的方法询问模型:

- (void)giveMeSomeData:(id)object withLabel:(id)label {
objectAsking= object;
theLabel= label;
NSThread* thread= [[NSThread alloc] initWithTarget:self selector:@selector(getTheDataFromInternet) object:nil];
[thread start];
}

- (void)getTheDataFromInternet {
//getting data...

theData= [dataFromInternet retain]; //this is the data the object asked for
[self returnObjectToAsker];
}

- (void)returnObjectToAsker {
[objectAsking receiveData:theData withLabel:theLabel];
}

由于我还是新手,你能告诉我它是不是一个好的模式?

谢谢!

1 个答案:

答案 0 :(得分:12)

您的设置非常正确。您永远不想在主线程上启动任何类型的网络连接。

目前的情况是,-returnObjectToAsker将在后台线程上执行。

您可能对-[NSObject performSelectorOnMainThread:withObject:waitUntilDone:]感兴趣。

或者如果您想要使用Grand Central Dispatch(iOS 4 +,Mac OS X 10.6+),您可以这样做:

#import <dispatch/dispatch.h>

- (void)giveMeSomeData:(id)object withLabel:(id)label {
    dispatch_async(dispatch_get_global_queue(0,0), ^{
      //this runs on a background thread
      //get data from the internet
      dataFromTheInternet = ...;
      dispatch_async(dispatch_get_main_queue(), ^{
        [object receiveData:dataFromTheInternet withLabel:label];
        //this runs on the main thread.  Use theData
      });
    });
}

由于块捕获了他们的环境,您甚至不必将objectlabel保存到ivars中。 :)