Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hiddenpremise

macrumors regular
Original poster
I am writing a Foundation tool that does some querying of opendirectory. The ODQuery object queries asynchronously so, my program exits before it returns anything. How do I stop my program from exiting until I receive a reply from my ODQuery?

Thanks,
Whit

PROBLEM RESOLVED
 
You should be a bit more specific for those of us who have not written a similar tool (Actually, i guess i have, but on a different platform with different tools).

Are you issuing a request, sleeping 1 second, checking for a result, then quitting? Couldn't you just put the check for result in a loop? If you're using some other approach, what is it? Can you post the code? at least some of it?

-Lee
 
@catfish_man: I don't understand semaphores well enough to implement them
[EDIT] Looked it up. Tried implementing an NSLock and unlocking in my callback from my query. Didn't work. Thread just exited after the method that calls the query was done[/EDIT]

@lee1210: running a loop delays activity in the NSRunLoop. The ODQuery object works by scheduling itself in a run loop. I don't know how to tell my program to check the runloop and wait to exit until the scheduled task has occurred.

I have tried spawning a separate thread to call the function that schedules my query. The thread exits after that function is complete, but before the query has finished running in [NSRunLoop currentRunLoop]
 
Schedule a timer in the runloop to keep it from exiting. Point the timer's fire method to a checker that looks for a response from the query.
 
@lee1210: running a loop delays activity in the NSRunLoop. The ODQuery object works by scheduling itself in a run loop. I don't know how to tell my program to check the runloop and wait to exit until the scheduled task has occurred.

Post your code that runs the NSRunLoop.

If you're not running the runloop, then post your code that uses or references the runloop in the main thread.

I have a feeling that you're not familiar with runloops. Without seeing code, there's no way to debug what it's doing now, and your descriptions are simply too vague to debug.
 
Schedule a timer in the runloop to keep it from exiting. Point the timer's fire method to a checker that looks for a response from the query.

This didn't help, because the thread exits after the method that i spawned it for finishes. Whatever in the run loop just gets dropped.
I spawn the thread like this
Code:
NSThread *queryThread = [[NSThread alloc] initWithTarget:checkAdmin selector:@selector(doQuery) object:nil];
	[queryThread start];
 
Post your code that runs the NSRunLoop.

If you're not running the runloop, then post your code that uses or references the runloop in the main thread.

I have a feeling that you're not familiar with runloops. Without seeing code, there's no way to debug what it's doing now, and your descriptions are simply too vague to debug.

My xCode Project is located here http://isis.berry.edu/whit/dirservice.zip

Thanks everyone for your help and suggestions so far
 
Your code never runs the runloop.

It adds and removes the ODQuery, but the runloop itself is never run.

In a Foundation Tool, no thread automatically runs its runloop, not even the main thread. That only happens in an application, and frankly, only because NSApplication does it (in short, it's not automatic, just built into NSApplication).

At a minimum, you need to add:
Code:
[[NSRunLoop currentRunLoop] run];
It should happen after:
Code:
    [myQuery scheduleInRunLoop: [NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

And get rid of the second thread entirely. You don't need it, and coordinating that thread, its runloop, and the main thread is 10X more complex than it needs to be. A basic runloop should work fine here.

And you should review the fundamentals of runloops, especially in the context of a Foundation Tool. If you're writing a tool that uses any of the async functionality of Cocoa, which is a considerable portion of its utility, then you will need to be extremely familiar with runloops.

Apple's low-level info on runloops is best described in its Threading Guide. This doesn't mean you need to create threads. You just need to treat the main thread the same way the Threading Guide describes for secondary threads.

http://developer.apple.com/iphone/l...ding/RunLoopManagement/RunLoopManagement.html

You should also read the NSRunLoop class reference, in particular the run method and its discussion of alternatives.

I can't point you to Apple example code, but there are plenty of searchable resources:

http://www.cocoabuilder.com/archive/search/1?q=runloop+foundation+tool&l=cocoa

http://www.cocoadev.com/index.pl?RunLoop

Google keywords: site:www.cocoadev.com run loop foundation tool


(The above may contain technical inaccuracies due to brevity, haste, and/or my own ignorance or indifference. Take them as general <handwave> outlines, not rigorous technical specifications.)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.