Stopping ODQuery (OpenDirectory Framework) takes a long time (unusable)
Stopping ODQuery (OpenDirectory Framework) takes a long time (unusable)
- Subject: Stopping ODQuery (OpenDirectory Framework) takes a long time (unusable)
- From: Andreas Känner <email@hidden>
- Date: Mon, 28 Sep 2009 12:07:17 +0200
Hi,
I want to search for LDAP contacts by using the new OpenDirectory
Framework API but this is not possible because I can't stop a query
immediately. I need to stop it when the user types in a different
search string. So I can setup a new query. To make things worse this
is all happening on the main thread and the application hangs until
the query is really stopped.
The duration to stop an ODQuery immediately after it has been created
and started is in my case 9 seconds! But I must say that this depends
on the network. My colleague has a faster network connection and he
measured a duration of 1 second.
Anyway, I think stopping the query should happen immediately (not
depending on network connections etc.)
If anyone knows a workaround please let me know.
Thanks,
Andreas
Here is my test code:
#import <Foundation/Foundation.h>
#import <OpenDirectory/OpenDirectory.h>
/*
LDAP Server : ldap.baylor.edu
LDAP Search Base : ou=People,o=Baylor University,c=US
LDAP Mappings : RFC 2307 (Unix)
No SSL, no username and no password.
(Configured with the Directory Utility)
*/
@interface MyQueryTester : NSObject <ODQueryDelegate>
{
ODSession* session;
ODNode* searchNode;
ODQuery* query;
}
- (void)startSearch:(NSString*)searchString;
- (void)stopSearch;
@end
@implementation MyQueryTester
- (id)init
{
if(self = [super init])
{
session = [ODSession defaultSession];
searchNode = [ODNode nodeWithSession:session
type:kODNodeTypeContacts error:nil];
}
return self;
}
- (void)stopSearch
{
if(query)
{
NSDate* date = [NSDate date];
[query removeFromRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[query release];
query = nil;
NSLog(@"time to stop the query: %f s", [[NSDate date]
timeIntervalSinceDate:date]);
}
}
- (void)dealloc
{
[self stopSearch];
[session release];
[searchNode release];
[super dealloc];
}
- (void)startSearch:(NSString*)searchString
{
[self stopSearch];
NSError* err = nil;
query = [[ODQuery alloc] initWithNode:searchNode
forRecordTypes:kODRecordTypePeople
attribute:kODAttributeTypeAllAttributes
matchType:kODMatchInsensitiveContains
queryValues:searchString
returnAttributes:kODAttributeTypeAllAttributes
maximumResults:0
error:&err];
if(query && !err)
{
query.delegate = self;
[query scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
}
}
- (void)query:(ODQuery*)theQuery foundResults:(NSArray *)results error:
(NSError *)error
{
BOOL hasFinished = !results && !error;
if(hasFinished)
{
NSLog(@"query is finished");
[self stopSearch];
}
else
NSLog(@"received nodes: %d", [results count]);
}
@end
void runForSeconds(NSTimeInterval seconds)
{
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate
dateWithTimeIntervalSinceNow:seconds]];
}
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString* searchString = @"Andrew";
// The first query stops itself after all nodes are found:
MyQueryTester* queryTester = [[MyQueryTester alloc] init];
[queryTester startSearch:searchString];
NSLog(@"started searching with query class: '%@'",
NSStringFromClass(queryClass));
runForSeconds(10.0);
NSLog(@"\n");
// The second query is stopped immediately after it has
been created and started,
[queryTester startSearch:searchString];
NSLog(@"started searching with query class: '%@'",
NSStringFromClass(queryClass));
NSLog(@"immediately stop the search ...");
// Stopping the query during operation takes a very long
time and the thread is blocked in the meantime!
[queryTester stopSearch];
runForSeconds(15.0);
[pool drain];
return 0;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden