在没有第二个线程的情况下调用 Objective-C 中延迟的函数
我想调用一个函数,即发送一个句子,延迟,以模拟数据流。数据存储在文本文件中。文本文件的每一行包含一个句子。我尝试通过使用 sleep(x) 早些时候获得延迟的调用行为,但这冻结了整个应用程序。我是否必须使用单独的线程,或者是否可以让它与 NSTimer 或其他东西一起工作。像 [self PerformSelector:@selector(parseSentence:) withObject:s afterDelay:2] 吗?
- (void) simulateStream
{
NSArray *sentences;
NSString *path = [[NSBundle mainBundle] pathForResource:@"Sentence_File" ofType:@"txt"];
NSString *st;
if (path)
{
st=[NSString stringWithContentsOfFile:pfad
encoding:NSUTF8StringEncoding
error:nil];
sentences=[[st substringFromIndex:[st rangeOfString:@"$"].location+1]
componentsSeparatedByString:@"$"];
}
for(int i=0; i<[sentences count]; i++)
{
//----CALL THIS WITH A DELAY OF 2 SECONDS----
[sentenceHandler parseSentence:[sentences objectAtIndex:i]];
}}
感谢您的帮助。问候
I want to call a function, that is sending a sentence, delayed, in order to simulate a stream of data. The data is stored in a textfile. Each line of the textfile contains one sentence. I tried to get a delayed calling behaviour earlier by using sleep(x) but this freezed the whole application. Do I have to use a seperate thread or is it possible to get it working with NSTimer or sth. like [self performSelector:@selector(parseSentence:) withObject:s afterDelay:2] ?
- (void) simulateStream { NSArray *sentences;
NSString *path = [[NSBundle mainBundle] pathForResource:@"Sentence_File" ofType:@"txt"]; NSString *st; if (path) { st=[NSString stringWithContentsOfFile:pfad encoding:NSUTF8StringEncoding error:nil]; sentences=[[st substringFromIndex:[st rangeOfString:@"$"].location+1] componentsSeparatedByString:@"$"]; } for(int i=0; i<[sentences count]; i++) { //----CALL THIS WITH A DELAY OF 2 SECONDS---- [sentenceHandler parseSentence:[sentences objectAtIndex:i]]; }}
Thanks for help. Greetings
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以增加每次传递的延迟间隔:
You could increase the delay interval in each pass: