Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Adding a delay before continuing?

Adding a delay before continuing?
Thread Tools
GeeYouEye
Junior Member
Join Date: Mar 2003
Location: Walnut Creek, CA
Status: Offline
Reply With Quote
Sep 15, 2004, 05:11 PM
 
I have a method which takes a passed string, writes it to a file in /tmp via [theString writeToFile:@"/tmp/file.txt"]

I then have two NSTasks to run on the newly created file, the latter of which is rm'ing it. When I run this stepping into each instruction via the debugger, everything runs smoothly. When I just run it, the output to the console is, for both tasks, "<task>: No such file or directory". This is leading me to believe that the file is not created in time to be processed or deleted (possibly due to Journaling on the HD?).

The end question is: what's the best way to make sure the file is actually created before moving on to the next instruction?
I bring order to chaos. You are in chaos windows, you are the contradiction, a bug wishing to be an OS.
     
Angus_D
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Sep 15, 2004, 05:35 PM
 
Are you sure the problem isn't that you're removing it before your first task has finished executing? Also, why are you using NSTask to execute rm? Why note just use the file removal functions in NSFileManager?

Take a look at NSTask's documentation. I believe there is a method that will wait until it's done.
     
3R1C
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Sep 15, 2004, 09:36 PM
 
Why even bother rm'ing it at all? is it huge or something? It'll go away on reboot automagically. Is that rude? To leave it there?
3R1C
     
Brass
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Sep 15, 2004, 10:52 PM
 
Why not use a "while" loop where you "stat" the file (or whatever the Obj-C equivalent is) to see if it exists?

When it exists, only then jump out of the loop and continue with the next steps.

This might help with debugging at the very least.
     
K++
Senior User
Join Date: Jan 2002
Location: NYC
Status: Offline
Reply With Quote
Sep 19, 2004, 08:17 PM
 
two things
1) You should be using NSTemporaryDirectory()
2) You should be using [NSFileManager removeFileAtPath:handler:
( Last edited by K++; Sep 20, 2004 at 03:18 PM. )
     
qyn
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status: Offline
Reply With Quote
Sep 19, 2004, 09:27 PM
 
The best way to control timing interaction between different threads is via NSLock. When a thread tries to acquire the lock, it will either take the lock (if it's the first one), or block (if it's the second one). When the first thread releases the lock, the second thread will unblock and all will be well. Just about the only things in cocoa that are guaranteed across threads are NSLocks, so you should use them liberally.
     
K++
Senior User
Join Date: Jan 2002
Location: NYC
Status: Offline
Reply With Quote
Sep 19, 2004, 09:36 PM
 
Originally posted by qyn:
The best way to control timing interaction between different threads is via NSLock. When a thread tries to acquire the lock, it will either take the lock (if it's the first one), or block (if it's the second one). When the first thread releases the lock, the second thread will unblock and all will be well. Just about the only things in cocoa that are guaranteed across threads are NSLocks, so you should use them liberally.
Not to rain on the locks parade, but those are unnecessary here since the NSTasks are returning control after execution. Also the problem wouldn't exist if he was using the right tools for the job, that I mentioned above.
     
GeeYouEye  (op)
Junior Member
Join Date: Mar 2003
Location: Walnut Creek, CA
Status: Offline
Reply With Quote
Sep 19, 2004, 11:09 PM
 
Sorry, I wasn't very (read: not at all) familiar with NSFileManager, whereas I was significantly more so with NSTask. You know the saying, when all you have is a screwdriver...

The file did have to be removed though in the course of the program, however, I've rewritten the relevant portion of the program so I no longer have to work with that temp file or any NSTask for that matter. Yay! Killed two birds with one stone there.

Only 1 more bird to go for consistent core functionality (but about the size of a California condor with the attitude of a Peregrine falcon (way to take a metaphor too far!)), and only two more (blue jays maybe) after that to finish this thing.
I bring order to chaos. You are in chaos windows, you are the contradiction, a bug wishing to be an OS.
     
Angus_D
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Sep 20, 2004, 11:33 AM
 
Originally posted by GeeYouEye:
Sorry, I wasn't very (read: not at all) familiar with NSFileManager, whereas I was significantly more so with NSTask. You know the saying, when all you have is a screwdriver...
But you don't have only a screwdriver. You have a hole toolbox, even if you didn't look hard enough to realise it was sitting under your nose. Use the right tool for the right job. Trust me, your life will be easier.
     
qyn
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status: Offline
Reply With Quote
Sep 20, 2004, 01:08 PM
 
Originally posted by K++:
Not to rain on the locks parade, but those are unnecessary here since the NSTasks are returning control after execution. Also the problem wouldn't exist if he was using the right tools for the job, that I mentioned above.
Yeah, my confusion. For some reason, I had it in my head that he was using NSThread, not NSTask. NSLocks do nothing for NSTask.

But in my defense, they really are great for threads!
     
GeeYouEye  (op)
Junior Member
Join Date: Mar 2003
Location: Walnut Creek, CA
Status: Offline
Reply With Quote
Sep 20, 2004, 06:30 PM
 
Originally posted by Angus_D:
But you don't have only a screwdriver. You have a hole toolbox, even if you didn't look hard enough to realise it was sitting under your nose. Use the right tool for the right job. Trust me, your life will be easier.
Perhaps a more apt analogy then would be: When all you know how to use is a screwdriver, every problem looks like a screw. I mean, I'm sure a power drill is great and all, but if I have no idea how to use it and my priority is getting a screw in a hole [getting my ideas coded at least preliminarily, despite the doubtless inefficiency], I'm going to use that screwdriver.
I bring order to chaos. You are in chaos windows, you are the contradiction, a bug wishing to be an OS.
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 12:35 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,