tags:

views:

64

answers:

1

Hi there.

I'm trying to search an LDAP database using the ODQuery method. I have the following code set up:

- (void)awakeFromNib
{
[self startSearch:@"john"];
}

- (void)startSearch:(NSString *)searchString
{
nodeName = @"http://sububria.org.au";
session = [ODSession defaultSession];
searchNode = [[ODNode alloc] init];
searchNode = [ODNode nodeWithSession:session name:nodeName error:NULL]; 

query = [[ODQuery alloc] initWithNode:searchNode
                       forRecordTypes:kODRecordTypePeople
                            attribute:kODAttributeTypeAllAttributes
                            matchType:kODMatchInsensitiveContains
                          queryValues:searchString
                     returnAttributes:kODAttributeTypeAllAttributes
                       maximumResults:0
                                error:NULL];

[query setDelegate:self];
[query scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)query:(ODQuery *)inSearch foundResults:(NSArray *)inResults error:(NSError *)inError
{ 
NSLog (@"Search ran");
NSLog (@"%@", inResults);
}

I'm pretty new to this so I'm not sure what I could be doing wrong. I'm not getting any warnings or errors in Xcode, my app is just crashing when the search query is run.

There is no console error that appears, but the most recent items in the thread stack are;

CFRetain
_ODQueryInitWIthNode
-[ODQuery initWithNode:forRecordTypes:attribute:matchType:queryValues:returnAttributes:maximumResults:error:]
-[MyAppDelegate startSearch:]
-[MyAppDelegate applicationDidFinishLaunching:]

I'd appreciate any help. Ricky.

A: 

All you've done is create a query; you haven't actually started a search.

To search synchronously, ask the query for all the results at once. If you pass NO (meaning return all the results), this may take a while.

To search asynchronously and get informed whenever it has more results for you, be the query's delegate and then schedule the query on a run loop.

Edit: Also, I doubt “http://server.org” is a valid node name. That's probably why node is nil.

Peter Hosey
Hi Peter. Thanks for your reply. I added the three appropriate lines of code, but my app still crashes on the initWithNode method. I have debugged my code and for some reason, searchNode has a memory address of 0x0, so it's been released. Where, I don't know. I've also tried retaining the variable but it's still is being deallocated somewhere. Ricky.
Ricky
Oh, I didn't see the bit about the crash. Please edit your question to include the crash log.
Peter Hosey
A variable containing `nil` (0x0) does not mean the object was deallocated; it means you never had one to begin with. An object being deallocated won't magically set any variables that had referred to it to `nil`; they'll still be referring to its old address. That's why it's important to make sure nothing still knows about (and might use) an object that nothing owns anymore (and so has died).
Peter Hosey
And you already own the query because you allocked it and have not released it. You don't need to retain it; doing so means that you own it twice. Review the memory management rules: http://developer.apple.com/mac/library/documentation/General/Conceptual/DevPedia-CocoaCore/MemoryManagement.html
Peter Hosey
Sorry Peter, I can't see why my searchNode object was never allocated. This is basically the exact same code that came from Apple's Open Directory Guide: http://developer.apple.com/mac/library/documentation/Networking/Conceptual/Open_Directory/workingWithRecords/workingWithRecords.html
Ricky
It most likely was allocated, and then released itself because you initialized it with bad values. See my edit to my answer, and my comment on your question.
Peter Hosey
I noted your comment above and I edited my code in my original post. I am still getting a crash when that initWithNode method is run. "http://server.org" is just something I put in this post. Although the real node in my code has a similar structure.
Ricky
You missed my point. I don't think an HTTP URL is a valid LDAP node name. And, again, please edit your question to include the crash log.
Peter Hosey
Hey Peter. I understand your point about the LDAP node name. I tried the IP address too with the /LDAPv3/ prefix. But that still isn't solving the crash and the fact that searchNode is 0x0. I updated my original post. Thanks again for your help.
Ricky