I am using amazon web services to upload images. I am not able to access a value out of the GCD block. How should i access the value of “myString” in some other class?
My code:
-(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response
{
AppDelegate *del = [[UIApplication sharedApplication]delegate];
if(indicatorView!=nil)
{
[indicatorView removeFromSuperview];
indicatorView=nil;
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Upload Completed" message:@"The image was successfully uploaded." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
[alert show];
//del.fileName = url;
// Set the content type so that the browser will treat the URL as an image.
/*S3ResponseHeaderOverrides *override = [[S3ResponseHeaderOverrides alloc] init];
override.contentType = @"image/jpeg";
// Request a pre-signed URL to picture that has been uplaoded.
S3GetPreSignedURLRequest *gpsur = [[S3GetPreSignedURLRequest alloc] init];
gpsur.key = @"NameOfThePicture";
//del.fileName = gpsur.key;
gpsur.bucket = @"pic-bucket";
gpsur.expires = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval) 3600]; // Added an hour's worth of seconds to the current time.
gpsur.responseHeaderOverrides = override;
// Get the URL
NSError *error;
url = [self.s3 getPreSignedURL:gpsur error:&error];
NSString *myString = [url absoluteString];
NSLog(@"myStringgg====>%@",myString);
del.fileName = myString;
NSLog(@"URL=====>%@",url);*/
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^
{
// Set the content type so that the browser will treat the URL as an image.
S3ResponseHeaderOverrides *override = [[S3ResponseHeaderOverrides alloc] init];
override.contentType = @"image/jpeg";
// Request a pre-signed URL to picture that has been uplaoded.
S3GetPreSignedURLRequest *gpsur = [[S3GetPreSignedURLRequest alloc] init];
gpsur.key = @"NameOfThePicture";
//del.fileName = gpsur.key;
gpsur.bucket = @"pic-bucket";
gpsur.expires = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval) 3600]; // Added an hour's worth of seconds to the current time.
gpsur.responseHeaderOverrides = override;
// Get the URL
NSError *error;
NSURL *url = [self.s3 getPreSignedURL:gpsur error:&error];
NSString *myString = [url absoluteString];
//del.fileName = myString;
NSLog(@"URL=====>%@",url);
if(url == nil)
{
if(error != nil)
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Error: %@", error);
//[self showAlertMessage:[error.userInfo objectForKey:@"message"] withTitle:@"Browser Error"];
});
}
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
//del.fileName = myString;
//NSLog(@"haURL===>%@",del.fileName);
});
del.fileName = myString;
}
//NSLog(@"nameURL====>%@",url);
});
}
Now i want to access “myString” var so that i can access its value in other class. Thanks
Property declarations:
@property(nonatomic,strong)NSString *fileName; //in appdelegate.h
@property(nonatomic,strong) NSString *File; //in the same controller as GCD
Update:
PictureViewController *pic = [[PictureViewController alloc]init];
**NSLog(@"newNameeee====>%@",pic.File);**
AppDelegate *del = [[UIApplication sharedApplication]delegate];
NSLog(@"LocatedAt====>%@",del.locatedAt);
NSLog(@"FileSize====>%d",del.fileSize);
**NSLog(@"FileName====>%@",del.fileName);**
The problem is that you try to access the
del.fileNamebefore the async part of therequest:didCompleteWithResponse:finishes.Try the following: modify your code slightly be moving the
del.fileName = myStringto the dispatch block on the main tread:Than you declare the
didReceivePictureFilenamein yourAppDelegateand move the accessing part of your code to that method. But you have to do the part where you start the upload some place else. (The code in your question doesn’t show that part)Of course you could also omit the
fileNameproperty entirely and hand the filename directly in thedidReceivePictureFilenamecallback: