NSOperations或NSThread用于连续相互取消的较小任务的突发? [英] NSOperations or NSThread for bursts of smaller tasks that continuously cancel each other?

查看:127
本文介绍了NSOperations或NSThread用于连续相互取消的较小任务的突发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查看是否可以针对网络服务进行搜索您的类型实施,并已针对其在iPhone上运行进行了优化。

I would like to see if I can make a "search as you type" implementation, against a web service, that is optimized enough for it to run on an iPhone.

这个想法是用户开始输入一个单词; Foo,每个新信后,我等待XXX毫秒。看看他们是否键入另一个字母,如果他们没有,我使用该词作为参数调用Web服务。

The idea is that the user starts typing a word; "Foo", after each new letter I wait XXX ms. to see if they type another letter, if they don't, I call the web service using the word as a parameter.

Web服务调用和随后的解析结果我想移动到一个不同的线程。

The web service call and the subsequent parsing of the result I would like to move to a different thread.

我写了一个简单的SearchWebService类,它只有一个公共方法:
- (void)searchFor:(NSString *) str;

I have written a simple SearchWebService class, it has only one public method: - (void) searchFor:(NSString*) str;

此方法测试搜索是否已在进行(用户输入时有一个XXX毫秒的延迟)停止搜索并启动一个新的。当结果准备就绪时,调用委托方法:

This method tests if a search is already in progress (the user has had a XXX ms. delay in their typing) and subsequently stops that search and starts a new one. When a result is ready a delegate method is called:

- (NSArray*) resultsReady;

我不知道如何获得这个功能'螺纹'。
如果我每次用户有XXX毫秒时都保持生成新线程。延迟打字我最终在一个坏的地方与许多线程,特别是因为我不需要任何其他搜索,但最后一个。
不是连续产生线程,我试着保持一个线程在后台运行的所有时间:

I can't figure out how to get this functionality 'threaded'. If I keep spawning new threads each time a user has a XXX ms. delay in the typing I end up in a bad spot with many threads, especially because I don't need any other search, but the last one. Instead of spawning threads continuously, I have tried keeping one thread running in the background all the time by:

- (void) keepRunning {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    SearchWebService *searchObj = [[SearchWebService alloc] init];
    [[NSRunLoop currentRunLoop] run]; //keeps it alive  
    [searchObj release];
    [pool release];
}

但我不知道如何访问searchFor searchObj对象,所以上面的代码工作并继续运行。我只是不能消息 searchObj 或检索 resultReady 对象?

But I can't figure out how to access the "searchFor" method in the "searchObj" object, so the above code works and keeps running. I just can't message the searchObj or retrieve the resultReady objects?

希望有人能指出正确的方向,线程是给我悲伤:)
谢谢。

Hope someone could point me in the right direction, threading is giving me grief:) Thank you.

推荐答案

好吧,我花了最后8小时读出每个例子。
我意识到,我将要做一些概念证明代码,看看是否甚至会有一个速度问题,为每个击键建立一个新的线程。

Ok, I spend the last 8 hours reading up on every example out there. I came to realize that I would have to do some "Proof of Concept" code to see if there even would be a speed problem with building a new thread for "each" keystroke.

事实证明,使用NSOperation和NSOperationQueue在速度方面,特别是在简单和抽象方面,已经足够了。

It turns out that using NSOperation and NSOperationQueue is more than adequate, both in terms of speed and especially in terms of simplicity and abstraction.

每个键击后调用:

- (void) searchFieldChanged:(UITextField*) textField {

    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    NSString *searchString = textField.text;

    if ([searchString length] > 0) {

        [self performSelector:@selector(doSearch:) withObject:textField.text afterDelay:0.8f];
    }
}

这主要是停止代码形式启动搜索对于小于800 ms的击键。分开。
(如果没有小的触摸键盘,我会有很多更低的)。

This is mainly to stop the code form initiating a search for keystrokes that are less than 800 ms. apart. (I would have that a lot lower if it where not for the small touch keyboard).

如果允许超时,搜索。

- (void) doSearch:(NSString*) searchString {

    [queue cancelAllOperations];
    ISSearchOperation *searchOperation = [[ISSearchOperation alloc] initWithSearchTerm:searchString];
    [queue addOperation:searchOperation];
    [searchOperation release];
}

取消当前队列中的所有操作。这是在每次新的搜索
开始时调用,它确保已经在进行中的搜索操作以有序的方式被关闭,它还确保只有一个线程处于未取消状态。

Cancel all operations that is currently in the queue. This is called every time a new search is started, it makes sure that the search operation already in progress gets closed down in an orderly fashion, it also makes sure that only 1 thread is ever in a "not-cancelled" state.

ISSearchOperation的实现非常简单:

The implementation for the ISSearchOperation is really simple:

@implementation ISSearchOperation

- (void) dealloc {

    [searchTerm release];
    [JSONresult release];
    [parsedResult release];
    [super dealloc];
}

- (id) initWithSearchTerm:(NSString*) searchString {

    if (self = [super init]) {

        [self setSearchTerm:searchString];
    }

    return self;
}

- (void) main {

    if ([self isCancelled]) return;
    [self setJSONresult:/*do webservice call synchronously*/];
    if ([self isCancelled]) return; 
    [self setParsedResult:/*parse JSON result*/];
    if ([self isCancelled]) return;

    [self performSelectorOnMainThread:@selector(searchDataReady:) withObject:self.parsedResult waitUntilDone:YES];
}

@end

从web服务下载数据和解析。
每次检查之后,如果搜索已被 [NSOperationQueue cancelAllOperations] 取消,那么我们返回并且该对象在dealloc方法。

There are two major steps, the downloading of the data from the web service and the parsing. After each I check to see if the search has been canceled by [NSOperationQueue cancelAllOperations] if it has, then we return and the object is nicely cleaned up in the dealloc method.

我可能需要为web服务和解析建立某种时间,以防止队列在一个 KIA 对象。

I will probably have to build in some sort of time out for both the web service and the parsing, to prevent the queue from choking on a KIA object.

但是现在这实际上是快速的,在我的测试中,我搜索一个16.000条目字典和Xcode NSLog它的屏幕慢下来很好),每800毫秒。我通过定时器发出一个新的搜索字符串,从而取消旧的,在它完成其NSLog结果到屏幕循环。
NSOperationQueue处理这个没有毛刺,永远不会超过几毫秒。的两个线程正在执行。该UI完全不受在后台运行的上述任务的影响。

But for now this is actually lightning fast, in my test I am searching an 16.000 entries dictionary and having Xcode NSLog it to the screen (slows things down nicely), each 800 ms. I issue a new search string via a timer and thereby canceling the old before it has finished its NSLog results to screen loop. NSOperationQueue handles this with no glitches and never more that a few ms. of two threads being executed. The UI is completely unaffected by the above tasks running in the background.

这篇关于NSOperations或NSThread用于连续相互取消的较小任务的突发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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