I'm sending some custom objects to a server from the app with ASIFormDataRequest inside a dispatch_async block. And when I receive OK from the server I need to put the object in a NSMutableArray. But when I try to access the the array from outside the block is empty.
My code pseudocode is like this:
-(void)sendObjects{
NSMutableArray *sendedObjects = [NSMutableArray alloc]init];
NSMutableArray *objectsToSend = [NSMutableArray alloc]init];
for(ObjectSend *obj in objectsToSend){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request appendPostData:[NSJSONSerialization dataWithJSONObject:sendDictWithObjects options:NSJSONWritingPrettyPrinted error:&error]];
[request startSynchronous];
NSError *requestError = [request error];
if(!requestError){
NSString *response = [request responseString];
obj.sended = YES;
[sendedObjects addObject:s];
}
});
}
NSLog(@"%i",sendedObjects.count);
}
And at the end the sendedObjects array is empty. I am using ARC, so can't retain. Somebody can help me?