Reputation: 21
I am trying to connect to AWS SQS to create a new queue and also fetch messages I've placed on a another queue. This is the code I'm using:
AWSSQS *sqs=[[AWSSQS alloc] initWithConfiguration:configuration];
AWSSQSCreateQueueRequest *createQueue = [[AWSSQSCreateQueueRequest alloc] init];
[createQueue setQueueName:@"TEST_Q2_NEW_3"];
BFTask *answer = [sqs createQueue:createQueue];
NSLog(@"Status queue creation: %@ %@", answer.result, answer.error);
AWSSQSReceiveMessageRequest *receiveMessageRequest = [[AWSSQSReceiveMessageRequest alloc] init];
receiveMessageRequest.QueueUrl = @"https://sqs.eu-west-1.amazonaws.com/xxxxxxxxxxxx/TEST_Q";
answer = [sqs receiveMessage:receiveMessageRequest];
NSLog(@"Status messages received %@ %@", answer.result, answer.error);
The queue TEST_Q2_NEW_3 is created but the log message is:
Status queue creation: (null) (null)
No messages are fetched but in the SQS Management Console some has the status Messages in Flight. However, the log message is:
Status messages received (null) (null)
What am I missing?
Upvotes: 1
Views: 425
Reputation: 21
Turns out I did not completely understand BFTasks. The statement:
answer = [sqs receiveMessage:receiveMessageRequest];
actually starts a process in the background and when the log message is printed this task has not yet finished. To continue the processing of the result you have to use a continueWithBlock. The working code looks like this for the receive message functionality:
answer = [sqs receiveMessage:receiveMessageRequest];
[answer continueWithBlock:^id(BFTask *task) {
if (task.isCancelled) {
NSLog(@"Status received messages %@ %@", answer.result, answer.error);
} else if (task.error) {
NSLog(@"Status received messages %@ %@", answer.result, answer.error);
} else {
NSLog(@"Status received messages %@ %@", answer.result, answer.error);
}
return nil;
}];
This is where I found the answer: BoltsFramework/Bolts-iOS - GitHub
Upvotes: 1