lloydsargent / BlackRaccoon

IOS FTP Client Code
102 stars 37 forks source link

Delegate not working properly #41

Closed LeenAlShenibr closed 10 years ago

LeenAlShenibr commented 10 years ago

I've implemented an FTPHelper class, that does the FTP communication for me using BlackRaccoon, however, all the delegate/required methods are not called. I'm trying to download a database file from my FTP server, it's not storing though, it did download it, since the data isn't communicated back requestCompleted. I'm sure that my FTP server is working, and that the data is being transferred because I placed print statements in BRStreamInfo.m. In my understanding, when the stream is complete it should call requestCompleted according to the following snippet

- (void)streamComplete:(BRRequest *)request
{
    NSLog(@"Stream Complete");
    [request.delegate requestCompleted: request];
    NSLog(@"Data tranferred back to FTP helper requestCompleted");
    [request.streamInfo close: request];
}

This is my FTP helper method, FTP request information are stored as constants:


#import "FTPHelper.h"
#import "Utility.h"

@implementation FTPHelper

- (IBAction) downloadFile: (id) sender
{
    downloadData = [NSMutableData dataWithCapacity: 1];

    downloadFile = [[BRRequestDownload alloc] initWithDelegate: self];

    //----- for anonymous login just leave the username and password nil
    downloadFile.path = path1;//Constant
    downloadFile.hostname = hostname1;//Constant
    downloadFile.username = username1;//Constant
    downloadFile.password = password1;//Constant

    NSLog(@"Download Started!!");
    //we start the request
    [downloadFile start];

}

- (void) uploadFile
{
    //----- get the file to upload as an NSData object
    uploadData = [NSData dataWithContentsOfFile: [Utility getDatabasePath]];

    uploadFile = [[BRRequestUpload alloc] initWithDelegate: self];

    //----- for anonymous login just leave the username and password nil
    uploadFile.path = path1;
    uploadFile.hostname = hostname1;
    uploadFile.username = username1;
    uploadFile.password = password1;

    //we start the request
    NSLog(@"Upload Starting.....");
    [uploadFile start];
}

- (IBAction) createDirectory:(id)sender
{
    createDir = [[BRRequestCreateDirectory alloc] initWithDelegate: self];

    //----- for anonymous login just leave the username and password nil
    createDir.path = path1;
    createDir.hostname = hostname1;
    createDir.username = username1;
    createDir.password = password1;

    //we start the request
    [createDir start];
}

- (IBAction) deleteDirectory:(id)sender
{
    deleteDir = [[BRRequestDelete alloc] initWithDelegate: self];

    //----- for anonymous login just leave the username and password nil
    deleteDir.path = path1;
    deleteDir.hostname = hostname1;
    deleteDir.username = username1;
    deleteDir.password = password1;

    //we start the request
    [deleteDir start];
}

- (IBAction) listDirectory:(id)sender
{
    listDir = [[BRRequestListDirectory alloc] initWithDelegate: self];

    //----- for anonymous login just leave the username and password nil
    listDir.path = path1;
    listDir.hostname = hostname1;
    listDir.username = username1;
    listDir.password = password1;

    [listDir start];
}

- (IBAction) deleteFile: (id) sender
{
    deleteFile = [[BRRequestDelete alloc] initWithDelegate: self];

    //----- for anonymous login just leave the username and password nil
    deleteFile.path = path1;
    deleteFile.hostname = hostname1;
    deleteFile.username = username1;
    deleteFile.password = password1;

    //----- we start the request
    [deleteFile start];
}

- (IBAction) cancelAction :(id)sender
{
    if (uploadFile)
    {
        //----- Remove comment if you do not want success delegate called
        //----- upon completion of the cancel
        // uploadFile.cancelDoesNotCallDelegate = TRUE;
        [uploadFile cancelRequest];
    }

    if (downloadFile)
    {
        //----- Remove comment if you do not want success delegate called
        //----- upon completion of the cancel
        // downloadFile.cancelDoesNotCallDelegate = TRUE;
        [downloadFile cancelRequest];
    }
}

- (void) requestDataAvailable: (BRRequestDownload *) request;
{
    [downloadData appendData: request.receivedData];
       NSLog(@"Received: %d", [request.receivedData length]);
}

-(BOOL) shouldOverwriteFileWithRequest: (BRRequest *) request
{
    //----- set this as appropriate if you want the file to be overwritten
    if (request == uploadFile)
    {
        //----- if uploading a file, we set it to YES
        return YES;
    }

    //----- anything else (directories, etc) we set to NO
    return NO;
}

- (NSData *) requestDataToSend: (BRRequestUpload *) request
{
    //----- returns data object or nil when complete
    //----- basically, first time we return the pointer to the NSData.
    //----- and BR will upload the data.
    //----- Second time we return nil which means no more data to send
    NSData *temp = uploadData;   // this is a shallow copy of the pointer

    uploadData = nil;            // next time around, return nil...

    return temp;
}

- (void) percentCompleted: (BRRequest *) request
{
    NSLog(@"%f completed...", request.percentCompleted);
}

-(void) requestCompleted: (BRRequest *) request
{
    NSLog(@"Transger Succesful!!");
    NSLog(@"REQUEST COMPLETED");
    //----- handle Create Directory
    if (request == createDir)
    {
        NSLog(@"%@ completed!", request);

        createDir = nil;
    }

    //----- handle Delete Directory
    if (request == deleteDir)
    {
        NSLog(@"%@ completed!", request);

        deleteDir = nil;
    }

    //----- handle List Directory
    if (request == listDir)
    {
        //----- called after 'request' is completed successfully
        NSLog(@"%@ completed!", request);

        //----- we print each of the file names
        for (NSDictionary *file in listDir.filesInfo)
        {
            NSLog(@"%@", [file objectForKey: (id) kCFFTPResourceName]);
        }

        listDir = nil;
    }

    //----- handle Download File
    if (request == downloadFile)
    {
        //called after 'request' is completed successfully
        NSLog(@"%@ completed!", request);

        NSData *data = downloadFile.receivedData;
        NSLog(@"Size of downloaded file is %d", data.length);

        //----- save the NSData as a file object
//        NSError *error;
        NSLog(@"Database path is %@", [Utility getDatabasePath]);
        NSLog(@"Size of dowbloaded database is %d", data.length);

        NSFileManager *fileManager = [NSFileManager defaultManager];

        [fileManager removeItemAtPath:[Utility getDatabasePath] error:nil];

        [data writeToFile: [Utility getDatabasePath] atomically:YES];
//        [data writeToFile: [Utility getDatabasePath] options: NSDataWritingFileProtectionNone error: &error];

        downloadFile = nil;
    }

    if (request == uploadFile)
    {
        NSLog(@"%@ completed!", request);
        uploadFile = nil;
    }

    if (request == deleteFile)
    {
        NSLog(@"%@ completed!", request);
        deleteFile = nil;
    }   
}

-(void) requestFailed:(BRRequest *) request
{
    NSLog(@"Request Failed");
    if (request == createDir)
    {
        NSLog(@"%@", request.error.message);

        createDir = nil;
    }

    if (request == deleteDir)
    {
        NSLog(@"%@", request.error.message);

        deleteDir = nil;
    }

    if (request == listDir)
    {
        NSLog(@"%@", request.error.message);

        listDir = nil;
    }

    if (request == downloadFile)
    {
        NSLog(@"%@", request.error.message);

        downloadFile = nil;
    }

    if (request == uploadFile)
    {
        NSLog(@"%@", request.error.message);

        uploadFile = nil;
    }

    if (request == deleteFile)
    {
        NSLog(@"%@", request.error.message);

        deleteFile = nil;
    }
}

@end

The header file:


#import <Foundation/Foundation.h>

//---------- include files
#import "BRRequestListDirectory.h"
#import "BRRequestCreateDirectory.h"
#import "BRRequestUpload.h"
#import "BRRequestDownload.h"
#import "BRRequestDelete.h"
#import "BRRequest+_UserData.h"

@interface FTPHelper : NSObject <BRRequestDelegate>
{
    BRRequestCreateDirectory *createDir;
    BRRequestDelete * deleteDir;
    BRRequestListDirectory *listDir;

    BRRequestDownload * downloadFile;
    BRRequestUpload *uploadFile;
    BRRequestDelete *deleteFile;

    NSMutableData *downloadData;
    NSData *uploadData;
}

- (IBAction)downloadFile: (id) sender;

- (void) uploadFile ;

@end

Any help regarding this issue is greatly appreciated.

LeenAlShenibr commented 10 years ago

I found out what the problem is, it turns out that the pointer delegate is destroyed during the download process. I changed it's property from (weak) to (strong) and now it works.

In BRRequest.h:

@property (weak) id <BRRequestDelegate> delegate;
//Change the above to the below
@property (strong) id <BRRequestDelegate> delegate;