当我真的需要使用 [NSThread sleepForTimeInterval:1]; [英] When I really need to use [NSThread sleepForTimeInterval:1];

查看:56
本文介绍了当我真的需要使用 [NSThread sleepForTimeInterval:1];的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要使用睡眠的程序.喜欢真的需要.与其花时间解释原因,只需说它需要它.

I have a program that needs to use sleep. Like really needs to. In lieu of spending ages explaining why, suffice to say that it needs it.

现在有人告诉我,如果需要睡眠,将我的代码拆分成一个单独的线程,这样我就不会失去界面响应能力,所以我开始学习如何使用 NSThread.我创建了一个全新的概念性程序,因此解决此示例的问题将对我的实际程序有所帮​​助.

Now I'm told to split off my code into a separate thread if it requires sleep so I don't lose interface responsiveness, so I've started learning how to use NSThread. I've created a brand new program that is conceptual so to solve the issue for this example will help me in my real program.

简短的故事是我有一个类,它有实例变量,我需要一个带有 sleep 的循环来依赖于该实例变量的值.

Short story is I have a class, it has instance variables and I need a loop with a sleep to be depended on the value of that instance variable.

无论如何,这是我整理的内容,非常感谢您的帮助:)

Here's what I've put together anyway, your help is very much appreciated :)

干杯

蒂姆

/// Start Test1ViewController.h ///
#import <UIKit/UIKit.h>

@interface Test1ViewController : UIViewController {
 UILabel* label;
}
@property (assign) IBOutlet UILabel *label;
@end
/// End Test1ViewController.h ///

/// Start Test1ViewController.m ///
#import "Test1ViewController.h"
#import "MyClass.h"
@implementation Test1ViewController
@synthesize label;
- (void)viewDidAppear:(BOOL)animated {
 [super viewDidAppear:animated];
 label.text = @"1";
 [NSThread detachNewThreadSelector:@selector(backgroundProcess) toTarget:self withObject:nil];
}

- (void)backgroundProcess {
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  

 // Create an instance of a class that will eventually store a whole load of variables
 MyClass *aMyClassInstance = [MyClass new]; [aMyClassInstance createMyClassInstance:(@"Timbo")];

 while (aMyClassInstance.myVariable--) {
  NSLog(@"blah = %i",aMyClassInstance.myVariable);
  label.text = [NSString stringWithFormat:@"blah = %d", aMyClassInstance.myVariable];

  //how do I pass the new value out to the updateLabel method, or reference aMyClassInstance.myVariable?
  [self performSelectorOnMainThread:@selector(updateLabel) withObject:nil waitUntilDone:NO]; 

  //the sleeping of the thread is absolutely mandatory and must be worked around.  The whole point of using NSThread is so I can have sleeps

  [NSThread sleepForTimeInterval:1];
 }
 [pool release];
}

- (void)updateLabel {label.text = [NSString stringWithFormat:@"blah = %d", aMyClassInstance.myVariable]; // be nice if i could }
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}
- (void)viewDidUnload {}
- (void)dealloc {[super dealloc];}
@end
/// End Test1ViewController.m ///

/// Start MyClass.h ///
#import <Foundation/Foundation.h>
@interface MyClass : NSObject {
 NSString* name;
 int myVariable;
}
@property int myVariable;
@property (assign) NSString *name;
- (void) createMyClassInstance: (NSString*)withName;
- (int) changeVariable: (int)toAmount;
@end
/// End MyClass.h ///

/// Start MyClass.h ///
#import "MyClass.h"
@implementation MyClass
@synthesize name, myVariable;
- (void) createMyClassInstance: (NSString*)withName{
 name = withName;
 myVariable = 10;
}
- (int) changeVariable: (int)toAmount{ 
 myVariable = toAmount;
 return toAmount;
}
@end
/// End MyClass.h ///

推荐答案

您可以像这样将 myVariable 值传递给主线程时对其进行包装:

You can wrap the myVariable value when passing it to the main thread like this:

[self performSelectorOnMainThread:@selector(updateLabel:) withObject:[NSNumber numberWithInt:aMyClassInstance.myVariable] waitUntilDone:NO]; 

然后,您必须像这样稍微修改 updateLabel 方法:

Then, you have to slightly modify the updateLabel method like this:

- (void)updateLabel:(NSNumber *)arg {
    int value = [arg intValue];
    label.text = [NSString stringWithFormat:@"blah = %d", value];
}

希望有帮助.

这篇关于当我真的需要使用 [NSThread sleepForTimeInterval:1];的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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