Skip navigation.
 
mlRe: Does this caution need fixed? (newb)
FROM : Brian Stern
DATE : Wed Jul 09 21:39:21 2008

On Jul 9, 2008, at 10:44 AM, Chris Paveglio wrote:

> I'm trying to make sure that this part of my code is good and clean 
> and not leaking. I believe that I have it right (but still think I 
> have something wrong).
> This code makes a list of file paths, and then copies the files or 
> folders from their location to the destination the user selected. If 
> the item already exists in the destination, I delete it first (to 
> overwrite with new data, yes this is what I intend and the user is 
> aware of how the app works).
> I've read all your previous responses (Thanks) and it seems like 
> since I am just using "normal" strings that they will be 
> autoreleased on their own. There are now no compiler cautions and 
> the app seems to work OK.
> A side issue is that I want to add a 2 second delay before closing 
> the sheet that is displayed, is NSTimer the correct item to use?
> Chris


I have some stylistic comments on this code.

Your method is too large.  To make it smaller you should break it up 
into a number of smaller tasks.
As Jens mentioned a string like this

NSString *string1  = @"Library/Preferences/Adobe Photoshop CS3 
Settings";

is pretty much the same as a string like this: @"Library/Preferences/
Adobe Photoshop CS3 Settings"

I would build a method that does nothing but return the array or 
arrays that contain your manifest of files to save

-(NSArray)myPrefsArray
{
   // build array here
   return myPrefs;
}

I notice that your  myPrefsSaved array is derived from the myPrefs 
array but it just has the file name not the full path.  I'd use 
[astring lastPathComponent] to either build the second array or just 
call this method inside your loop.

I would break out the entire copy files loop into its own method.

Since you loop over the contents of the myPrefs array I wouldn't hard 
code the loop index to 8 or 9 I'd depend on the [myrefsArray length] 
value to index the loop.  You should also look at NSEnumerator and 
also fast enumeration as ways to drive this loop in a better way.

I notice that you call [myPrefs objectAtIndex:i] several times in your 
loop.  If you use NSEnumerator or fast enumeration you can avoid this 
otherwise just call it once at the top of the loop and save the value 
in a temp string variable.

Also, declaring the loop counter outside the loop is now old 
fashioned.  You can try this:

for (int i = 0; i < limit; i++)

You might need to set your C dialect setting to C99 or gnu99 if you're 
using an old project. I recommend it.

You don't check any errors from the file manager operations. What if 
the user chooses a folder on a locked volume or the disk gets full or 
the user chooses a network volume and the network drops out?  Your 
users will hate you if these things happen but your app fails 
silently.  You should both check for errors and report them to the user.

I don't see any problems in this code with leaking of objects.

I don't like the way that all the steps of this process are done one 
after another in a sequential fashion.  I would probably set up the 
tasks to be done in a separate thread or have each file copy be done 
inside a timer callback until all are done and show a progress window 
probably in place of a progress sheet (although it might work ok 
either way).

Regarding your 2 second delay you can use a timer or 
performSelector:withObject:afterDelay but both will require you to 
provide a callback method that will dismiss your sheet.

Good luck,

Brian

Related mailsAuthorDate
mlDoes this caution need fixed? (newb) Chris Paveglio Jul 3, 18:40
mlRe: Does this caution need fixed? (newb) Andy Lee Jul 3, 19:35
mlRe: Does this caution need fixed? (newb) Kyle Sluder Jul 3, 19:47
mlRe: Does this caution need fixed? (newb) Chris Paveglio Jul 3, 20:57
mlRe: Does this caution need fixed? (newb) Sherm Pendley Jul 3, 21:08
mlRe: Does this caution need fixed? (newb) Jason Stephenson Jul 3, 21:22
mlRe: Does this caution need fixed? (newb) Michael Watson Jul 3, 21:29
mlRe: Does this caution need fixed? (newb) Andy Lee Jul 3, 21:43
mlRe: Does this caution need fixed? (newb) Chris Paveglio Jul 3, 22:09
mlRe: Does this caution need fixed? (newb) Steve Christensen Jul 3, 22:46
mlRe: Does this caution need fixed? (newb) Steve Christensen Jul 3, 22:53
mlRe: Does this caution need fixed? (newb) Sean McBride Jul 3, 23:04
mlRe: Does this caution need fixed? (newb) Jason Stephenson Jul 4, 02:22
mlRe: Does this caution need fixed? (newb) Chris Paveglio Jul 9, 16:44
mlRe: Does this caution need fixed? (newb) Jens Alfke Jul 9, 17:27
mlRe: Does this caution need fixed? (newb) Brian Stern Jul 9, 21:39