Problem - Functions looping for no reason

Joined
Mar 18, 2009
Messages
2
Reaction score
0
Points
1
I am a very new developer to Objective-C. I have no idea what I'm doing. I wrote this program as a proof of concept GUI command line wrapper. The compressZip and compressTar functions work fine, and the initComp when called from compressZip works fine as well same with initTar respectively. However, when finishedTar (which is called by initTar) calls either compressBZip2 or compressGZip, the functions start looping, and it's not only the compress functions. The finishedCompress starts looping along with the initComp function. I already made a version where NSLog logged every start and end of a function but I couldn't figure out a pattern (not saying much though). This version is clean and has no NSLogs. If you have the time, please take a look (I mean a lot of time - I read the source for hours!). Sorry if this is confusing.
NOTE: DO NOT RUN THE BZIP2 OR GZIP COMPRESSION WITHOUT CONSTANT MONITORING AND XCODE AT THE READY TO KILL THE TASK. IT WILL COMPLETELY LOCK UP YOUR COMPUTER IF LEFT RUNNING ON THOSE TWO TASKS FOR MORE THAN 20SECS (WON'T EVEN LET YOU FORCE QUIT).

Code:
//AppController.h
#import <Cocoa/Cocoa.h>

@interface AppController : NSObject {
    IBOutlet id cmdCompress;//PushButton
    IBOutlet id mnuCompression;//Popup Menu (NSPopupButton)
    IBOutlet id progressBar;//Progress Indicator
    IBOutlet id txtPath;//Text box
	NSTask *tarFile, *compFile;
	NSString *targetPath, *archiveName;
	NSInteger compressionType;
}
- (IBAction)compressFiles:(id)sender;
- (id)compressTar;
- (id)compressZip;
- (id)compressBZip2;
- (id)compressGZip;
- (id)initTar;
- (id)initComp;
- (void)finishedTar:(NSNotification *)aNotification;
- (void)finishedCompress:(NSNotification *)aNotification;
@end

/////////////////////////////////
//AppController.m
#import "AppController.h"

@implementation AppController
- (IBAction)compressFiles:(id)sender {
    [cmdCompress setTitle:@"Running..."];
	[cmdCompress setEnabled:NO];
	[progressBar startAnimation: self];
	targetPath = [[txtPath stringValue] stringByExpandingTildeInPath];
	compressionType = [mnuCompression indexOfSelectedItem];
	if (compressionType == 0) {
		[self compressZip];
	}
	else {
		[self compressTar];
	}
}
- (id)compressZip {
	[self initComp];
	archiveName = [targetPath stringByAppendingString:@".zip"];
	compFile = [[NSTask alloc] init];
	[compFile setLaunchPath:@"/usr/bin/zip"];
	[compFile setCurrentDirectoryPath:@"/"];
	[compFile setArguments:[NSArray arrayWithObjects:@"-r",archiveName,targetPath,nil]];
	[compFile launch];
	return self;
}

- (id)compressTar {
	[self initTar];
	archiveName = [targetPath stringByAppendingString:@".tar"];
	tarFile = [[NSTask alloc] init];
	[tarFile setLaunchPath:@"/usr/bin/tar"];
	[tarFile setCurrentDirectoryPath:@"/"];
	[tarFile setArguments:[NSArray arrayWithObjects:@"-cf",archiveName,targetPath,nil]];
	[tarFile launch];
	return self;
}
- (id)compressBZip2 {
	[self initComp];
	compFile = [[NSTask alloc] init];
	[compFile setLaunchPath:@"/usr/bin/bzip2"];
	[compFile setCurrentDirectoryPath:@"/"];
	[compFile setArguments:[NSArray arrayWithObject:archiveName]];
	[compFile launch];
	return self;
}
- (id)compressGZip {
	[self initComp];
	compFile = [[NSTask alloc] init];
	[compFile setLaunchPath:@"/usr/bin/gzip"];
	[compFile setCurrentDirectoryPath:@"/"];
	[compFile setArguments:[NSArray arrayWithObject:archiveName]];
	[compFile launch];
	return self;
}

- (id)initTar {
	self = [super init];
    [[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(finishedTar:) 
												 name:NSTaskDidTerminateNotification 
											   object:nil];
	
	tarFile = nil; // This is a good time to initialize the pointer
	return self;
}
- (id)initComp {
	self = [super init];
    [[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(finishedCompress:) 
												 name:NSTaskDidTerminateNotification 
											   object:nil];
	
	compFile = nil; // This is a good time to initialize the pointer
	return self;
}

- (void)finishedTar:(NSNotification *)aNotification {
	[tarFile release];
	tarFile = nil;
	if (compressionType == 1) {
		[self compressBZip2];
	}
	else if (compressionType == 2) {
		[self compressGZip];
	}
}
- (void)finishedCompress:(NSNotification *)aNotification {
	[compFile release];
	compFile = nil;
	[progressBar stopAnimation: self];
	[cmdCompress setEnabled:YES];
	[cmdCompress setTitle:@"Compress Files"];
}
@end
 
OP
G
Joined
Mar 18, 2009
Messages
2
Reaction score
0
Points
1
Fixed it!

sorry, I got it.
Can't use two NSNotification
Just used one and an if statement.
Thanks,
Robert
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top