Pregunta

I have a NSTableview with three columns 'Thumbnail','FileName' and 'Datemodified'. The file name column and date modified column is filled with the output of S3ListObjectResponse. While the 'Thumbnail' column image is loaded from the local directory of the machine. If the file is missing, I download it from the S3 cloud. The problem is how to insert the data into the NSArrayController if the file is missing and I'm left with an empty cell for the thumbnail? Here is my work as of now :

- (IBAction)checkCloud:(id)sender {
AmazonS3Client *s3 = [AmazonClientManager s3];

S3ListObjectsRequest* listObjectsRequest = [[S3ListObjectsRequest alloc] initWithName:@"hello-testing"];



NSRange range = NSMakeRange(0, [[_fileListAC arrangedObjects] count]);
[_fileListAC removeObjectsAtArrangedObjectIndexes:[NSIndexSet indexSetWithIndexesInRange:range]];

@try {


    S3ListObjectsResponse* response = [s3 listObjects:listObjectsRequest];

    NSMutableArray* objectSummaries = response.listObjectsResult.objectSummaries;

    //looping through the objSummary and add into the NSArrayController
    for ( S3ObjectSummary* objSummary in objectSummaries ) {

        NSImage *thumbnail = [[NSImage alloc] init ];


        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];


        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];

        NSDate *s3date = [dateFormatter dateFromString:[objSummary lastModified]];


        NSArray *fileName =  [[objSummary key] componentsSeparatedByString:@"/"];
        if([[fileName objectAtIndex:0] localizedCaseInsensitiveCompare:@"Range"] == NSOrderedSame) {
            NSLog(@"file name %@ ",[fileName objectAtIndex:1]);

           // Check for files in side the bucket's folders 
           if([[fileName objectAtIndex:1] length]){

                NSString *thumbnailFile = [[fileName objectAtIndex:1] stringByAppendingString:@"_thumb.png"];


                BOOL isDir;

                NSFileManager *fileManager = [NSFileManager defaultManager];

                NSString *filePathPart1 = [@"/Users/" stringByAppendingString:[[NSHost currentHost] localizedName]];

                NSString *filePath = [[[[ filePathPart1 stringByAppendingString:@"/Documents/Test/" ] stringByAppendingString:@"Range" ] stringByAppendingString:@"/Thumbnail/"] stringByAppendingString:thumbnailFile];


               // If file exists then add into the thumbnail column
               if([fileManager fileExistsAtPath:filePath isDirectory:&isDir]){

                    thumbnail = [[NSImage alloc] initWithContentsOfFile:filePath];



                }else{

                     NSLog(@"file %@ not found",thumbnailFile);


                    //Downloading from the S3 Cloud Asynchronously

                    doneDownload = NO;
                    AmazonS3Client *s3 = [AmazonClientManager s3];
                    S3GetObjectRequest *gor = nil;
                    @try {

                        gor = [[S3GetObjectRequest alloc] initWithKey:[[@"Range/Thumbnail/"  stringByAppendingString:[fileName objectAtIndex:1]] stringByAppendingString: @"_thumb"] withBucket:@"thumbnail-pic" ];
                        gor.delegate = self;
                        [s3 getObject:gor];
                    }
                    @catch (AmazonClientException *exception) {
                        doneDownload = YES;
                    }

                    do {
                        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
                    } while (!doneDownload);
                    gor.delegate = nil;

                }

            }else{
                //If its the actual bucket folder and not file then load a default image 
                thumbnail = [NSImage imageNamed:@"Range.png"];
            }

        }

           [_fileListAC addObject:[NSMutableDictionary  dictionaryWithObjectsAndKeys:thumbnail,@"thumbnail", [objSummary key],@"Files",s3Date,@"Date Modified", nil]];





    }
}
@catch (NSException *exception) {
    NSLog(@"Cannot list S3 %@",exception);
}

//Display the table on loading the NSArrayController
[_cloudFileList makeKeyAndOrderFront:nil]; 

}

I have checked the delegate method

 -(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response{
NSData *imageData = [response body];
_coreDataImageView.image = [[NSImage alloc] initWithData:imageData];
doneDownload = YES;
}

coredataimageview is a test image well. The image is downloaded but I want to add it back to the array controller. Issue is what if I have 3 file missing from the local directory and I'm unable to insert into array controller for that file name and date modified values. Any help on this?

¿Fue útil?

Solución

If you implement a delegate object (rather than using self) you could store these objects in an array and reference them later. The delegate object could track the additional pieces of metadata you are interested in and could be used to construct your table view.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top