RE: Blocking Method Return
RE: Blocking Method Return
- Subject: RE: Blocking Method Return
- From: "Josh Ferguson" <email@hidden>
- Date: Fri, 2 Dec 2005 07:24:36 -0600
- Thread-topic: Blocking Method Return
So it looks like the method below was the proper way to go about this in
the first place, but I'm running into an issue with my window still. In
my code, I call [NSApp loadNibNamed:owner:] to load the nib that has my
window, and then display the window using makeKeyAndOrderFront. After
doing this, it doesn't seem to be able to receive any events. I've tried
4 different approaches which give me two different results:
While(!networking done && [[NSRunLoop currentRunLoop]
runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture];
{
// wait
}
AND
While(!networking)
{
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 100000, TRUE);
}
Both of these methods allow the window to load (and my delegate methods
do get called). However, while the page is displayed correctly, the
window won't receive any events. I can't scroll, interact with the
WebView, or even close the window.
The other method I tried was this:
Calling CFRunLoopRun() after loading the window and then calling
CFRunLoopStop() when I'm finished with everything.
AND/OR:
NSEvent *event;
while(event = [_window nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]
inMode:NSDefaultRunLoopMode]
{
[_window sendEvent:event];
}
Both of these immediately return without processing any events. Is there
something that would cause the runloop to change when loading a bundle
and window this way?
Thanks in adavance,
Josh Ferguson
-----Original Message-----
From: cocoa-dev-bounces+josh=email@hidden
[
mailto:cocoa-dev-bounces+josh=email@hidden] On Behalf
Of Josh Ferguson
Sent: Thursday, December 01, 2005 4:23 PM
To: Shawn Erickson
Cc: email@hidden
Subject: RE: Blocking Method Return
Shawn,
Thanks for the reply. I should've given more background. Essentially,
here's where I'm at today:
- (void)method1
{
<initiate asynchronous networking>
While(!networking done && [[NSRunLoop currentRunLoop]
runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture];
{
// wait
}
<more networking>
While(!networking done && [[NSRunLoop currentRunLoop]
runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture];
{
// wait
}
<Display Window with webview>
While(!networking done && [[NSRunLoop currentRunLoop]
runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture];
{
// wait
}
}
With this code, all the callbacks and asynchronous networking function
properly and the webview renders the first page - however, after that,
the window is not receiving any events. It just sits there and I can't
scroll it or close it or anything.
I also thought of using the [NSWindow nextEventMatchingMask:] method to
simulate a modal dialog (which is what I eventually want, but you can't
have a webview in a modal dialog using runModal...), but this crashes my
app (not sure why yet). What I mean by my library having a UI is that
when a library function is called it dynamically loads a framework and
that framework may contain UI (as in the example above).
I understand the general concepts of RunLoops and Threading, what I'm
looking for, I guess, is a runloop that'll allow my window to process
events while it's being run from the loop in method1.
Thanks!
Josh
-----Original Message-----
From: Shawn Erickson [
mailto:email@hidden]
Sent: Thursday, December 01, 2005 4:11 PM
To: Josh Ferguson
Cc: email@hidden
Subject: Re: Blocking Method Return
On 12/1/05, Josh Ferguson <email@hidden> wrote:
>
I'm currently writing a library that implements some asynchronous
networking code and performs a series of tasks. I want the entrypoint
for this library to be synchronous - so that the method doesn't return
until all the networking is done. Here's my specific scenario:
>
>
1.) Third party app calls into -[MyController method1]
>
>
2.) -method1 creates an asynchronous NSURLConnection and downloads
some data
>
>
3.) Because NSURLConnection is asynchronous (which I need for
progress updates), method1 returns immediately.
>
>
Step 3 is what I need to change. I don't want my method1 to return
until all the asynchronous networking is done.
This implies you want to block (or possibly spin in) the thread that
called your "method1" down in "method1" until some asynchronous
operation completes.
>
To complicate matters, there's also UI involved (a WebView may be
displayed). How do I go about blocking method1 until we're completely
finished, without blocking the entire thread?
You may want to grab a book or review online resources on threads
since the above question doesn't make sense. :)
A thread is either running (executing or waiting to be scheduled) or
blocked. The thread will run the sequence of instructions that it is
presented with. In this case some call graph resulted in your method1
being called by this thread, the thread will either have to block in
your method1 or spin (remain running) in your method1 if you don't
want to return out of method1 until your asynchronous operation
completes.
If the thread that called method1 is the main thread of the
application (assuming a standard Cocoa application) then blocking this
thread will stall the processing of events for this application. If
stalled long enough you will see the spinning beach ball and/or the
application will respond poorly to users. If you document that method1
of library will block then callers should take steps to ensure that
the main thread isn't used to call this method.
Also if your code is depending on the main thread (having its runloop
going) then blocking could prevent notification of completion of your
asynchronous operation and/or prevent any UI you display from
functioning.
Another option is to spin in your method1 in a way that allows it to
potentially process events. You could do this by getting the threads
current runloop ([NSRunLoop currentRunLoop]) and then run that runloop
(runloops are basically a glorified loop, in other words they spin).
You would want to document this behavior to callers since it is a
little abnormal todo.
>
I'm sure there's a simple answer - I'm just not sure at all how to
tackle the problem.
It will be hard to answer fully and correctly without a better
understanding of what you are doing.
For one I am confused on what you mean in above about your library
having a UI... basically I am not sure how your library is expected to
be used by a third party.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden