Re: wait for the event?
Re: wait for the event?
- Subject: Re: wait for the event?
- From: Matthew Mashyna <email@hidden>
- Date: Mon, 5 Oct 2009 21:45:20 -0400
Well, all I can say is that you need some time to come around to a
different way of thinking. You are thinking too linearly. You need to
set up your loads and listens and coordinate them.
You don't want to design your app to sit and spin on the main thread.
It's job is to handle all the basic events. If I were you I would have
my app fire up a thread to kick off these web pages you need to load.
I would create a new class of object that manages these web loads and
register each of them to each listen for just one webview. Don't have
you main class listen have these other objects listen.
Have your processing thread -- the one that starts the webloads -- set
a lock for each of the manager objects. These "manager" objects can
handle the post-load processing and clear the lock. When all the locks
are cleared then your processing thread moves on.
So, what I'm saying is: don't do the run loop in the main thread
manually like you want to. Resist the urge. Control things from a
secondary thread that doesn't mind being blocked until the locks are
cleared.
What you are describing with the flagging and the data arriving out of
sync is the classic, text book example of when to use locks. You have
a resource (the loaded web pages) that another object wants to
consume. Look at the NSLock documentation. I think that will help you.
Good luck,
Matt
On Oct 5, 2009, at 8:34 PM, jon wrote:
well there seems to be a problem, I do the main Method like so,
- (void)loadThePage
{
URLToLoad = [NSURL URLWithString:theUrlString];
[offScreenWebView setFrameLoadDelegate:self];
[[offScreenWebView mainFrame] loadRequest:[NSURLRequest
requestWithURL:URLToLoad]];
}
@end
and i put the rest in the selector method like so... to be executed
when the page is fully loaded, which works... but the thing is,
the code immediately following the main call to "loadThePage" isn't
waiting, it keeps on executing... which defeats the purpose, the
whole program needs that data.....
//
-------------------------------------------------------------------------------
- (void) webViewProgressFinished:(NSNotification*)notification
{
didChange = NO;
DOMDocument *myDOMDocument = [[offScreenWebView mainFrame]
DOMDocument];
....... etc............
if the webpage was quick to load it works.... in reality the page
takes a while to load, and in the mean time, the rest of my
application (the code after the "loadThePage" keeps on executing so
i have not successfully controlled the running of the app., it is
executing methods left and right, without the
finishLoadingTheWebPage ever being called until half the application
has already executed...
putting the loading into another thread results in the same thing,
the rest of the application needs the data, so it can not be
running until the webPage is fully loaded...
so i have to use a combination, i need to put in a flagging type of
thing, and have the runLoop wait for that flag, the flag needs to
be in the "webViewProgressFinished" to work (it was not before)..
yet i still need that flag....
thanks,
Jon.
On Oct 5, 2009, at 3:46 PM, Matthew Mashyna wrote:
On Oct 5, 2009, at 12:51 PM, jon wrote:
i have a webView loading, and need to wait until it is finished
loading...
so far i've set up this notification in the wakeFromNib:
NSNotificationCenter *center = [NSNotificationCenter
defaultCenter];
[center addObserver:self
selector:@selector(webViewProgressFinished:)
name:WebViewProgressFinishedNotification object:offScreenWebView];
and then i have a selector/method like so, with nothing in it,
because i'm not sure what to put in it yet...
- (void)webViewProgressFinished:(NSNotification*)notification
{
NSInteger i = 1;
}
and then i have a the main method that is running along, and at a
certain point below i need to wait for that notification to
fire... Or that method to fire... same thing...
i need a little push in the correct direction, because i really
am stumped in whether to use some sort of NSRunLoop, or NSEvent,
or what would it be?
URLToLoad = [NSURL URLWithString:theUrlString];
[offScreenWebView setFrameLoadDelegate:self];
[[offScreenWebView mainFrame] loadRequest:[NSURLRequest
requestWithURL:URLToLoad]];
[[NSRunLoop currentRunLoop] runUntil... we get that darn message?];
here is where i need to wait until i get that message, or that
that method is fired...
I seem to have the selector firing correctly i believe ... but i
don't know how to wait until it fires?
that is what i have so far...
and help would be appreciated greatly...
thanks,
Jon.
In general you don't want to wait for, uh what did you say? a
selector to fire? You start an asynchronous operation (like
loadRequest) and let the current run loop notify you. Exit the
method after you do your loadRequest. When you get the
notification, do your thing to finish it in webViewProgressFinished
-- do the thing that you were waiting to do.
I could be wrong but I think you might actually be blocking the
notification by doing the sit 'n spin bit with [[NSRunLoop
currentRunLoop] runUntil... we get that darn message?];
Is there a reason you have to loop and wait for it to finish
instead of doing more work in the notification method? If there is
then maybe you should use a lock to synchronize the threads instead
of waiting.
You might want to tell us more about what you're trying to do.
Matt
_______________________________________________
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