Sending AsynchronousRequest with a block-the right way - objective-c

In my app i am sending a request to server .
The request is in some other class-called requestClass, and is being called from the main view class. (i am using cocos2d).
My question is, how would i informed the main class (from the requestClass ) that the operation is done ?
When it finish the request-its callback is in its own class(requestClass) and the NSLog is done IN the requestClass .
i dont think NSNotification is the right way
requestClass is like :
[NSURLConnection
sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error)
{
if ([data length] >0 && error == nil)
{
**// HOW SHOULD I INFORM THE CLASS THAT CALL ME NOW ???**
}
else if ([data length] == 0 && error == nil)
{
NSLog(#"Nothing ");
}
else if (error != nil){
NSLog(#"Error = %#", error);
}
}];

OK, to write a delegate protocol...
Assuming your connection file is called MyRequestClass.
In MyRequestClass.h...
#protocol MyRequestClassDelegate <NSObject>
- (void)requestDidFinishWithDictionary:(NSDictionary*)dictionary;
//in reality you would pass the relevant data from the request back to the delegate.
#end
#interface MyRequestClass : NSObject // or whatever it is
#property (nonatomic, weak) id <MyRequestClassDelegate> delegate;
#end
Then in MyRequestClass.h
[NSURLConnection
sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error)
{
if ([data length] >0 && error == nil)
{
[self.delegate requestDidFinishWithDictionary:someDictionary];
//you don't know what the delegate is but you know it has this method
//as it is defined in your protocol.
}
else if ([data length] == 0 && error == nil)
{
NSLog(#"Nothing ");
}
else if (error != nil){
NSLog(#"Error = %#", error);
}
}];
Then in whichever class you want...
In SomeOtherClass.h
#import "MyRequestClass.h"
#interface SomeOtherClass : UIViewController <MyRequestClassDelegate>
blah...
In someOtherClass.m
MyRequestClass *requestClass = [[MyRequestClass alloc] init];
requestClass.delegate = self;
[requestClass startRequest];
... and make sure to write the delegate function too...
- (void)requestDidFinishWithDictionary:(NSDictionary*)dictionary
{
//do something with the dictionary that was passed back from the URL request class
}

Related

Creating NSError * results in application crash

I have the following situation:
My domain class gets some input validates it, and if validation passes it proceeds with saving data.
When control flow reaches the if statements, the application crashes
- (BOOL)createGmailAccountWithName:(NSString *)name
email:(NSString *)email
andPassword:(NSString *)password
error: (NSError **) error {
if (!name || name.length == 0) {
*error = [self createError:#"name"];
return NO;
}
if (!email || email.length == 0) {
*error = [self createError:#"email"];
return NO;
}
if (!password || password.length == 0) {
*error = [self createError:#"password"];
return NO;
}
//..
}
-(NSError *) createError: (NSString *) field {
NSString *errorMessage = [NSString stringWithFormat:#"Property %# is required", field];
NSDictionary *userInfo = #{
NSLocalizedFailureReasonErrorKey: NSLocalizedString(errorMessage, nil)
};
NSError *error = [NSError errorWithDomain:ACCOUNT_STORE_ERROR_DOMAIN
code:-1
userInfo:userInfo];
return error;
}
When I comment out all lines where the validation happens, the application does not crash.
I have no idea why this is happening. Can anyone point me into the right direction?
If you have this method:
- (BOOL)createGmailAccountWithName:(NSString *)name
email:(NSString *)email
andPassword:(NSString *)password
error: (NSError **) error
Folks are probably going to call it either like this:
NSError *error;
[accountCreator createGmailAccountWithName:#"Ben"
email:#"foo#example.com"
andPassword:#"pwd"
error:&error];
if (error)
{
NSLog(#"Hey I got an error: %#", error);
}
Or like this:
[accountCreator createGmailAccountWithName:#"Ben"
email:#"foo#example.com"
andPassword:#"pwd"
error:NULL];
// I couldn't care less about an error
In the second case, your code will will try to dereference **error, *error is not a valid pointer and would cause a crash.

objective C process other events within while loop

Is there a way to force your application to process other events? I have a loop that is supposed to wait 10 seconds for a json response. The problem seems to be that the loop processes before the didReceiveData event can run.
JsonArray is an NSArray property.
Here is my code in Session.m:
-(BOOL)logIn
{
JsonArray = nil;
if (([Password class] == [NSNull class]) || ([Password length] == 0))
return NO;
if (([Username class] == [NSNull class]) || ([Username length] == 0))
return NO;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL_ALL_PROPERTIES]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
if (!connection)
{
return NO;
}
int time = CFAbsoluteTimeGetCurrent();
while (!JsonArray)
{
if (CFAbsoluteTimeGetCurrent() - time == 10)
{
NSLog(#"breaking loop");
break;
}
// process events here
}
if (!JsonArray) return NO;
NSLog(#"JsonArray not nil");
return YES;
}
didReceiveData handler:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(#"Data received");
NSError *e = nil;
JsonArray = nil;
JsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];
if (!JsonArray)
{
JsonArray = nil;
Connected = NO;
return;
}
Connected = YES;
}
Embrace the asynchronous nature of what you're trying to do. Remove the return from the login method and add a completion block instead. Store the completion block in a property (copy) and call it from connectionDidFinishLoading:.
typedef void (^MYCompletionHandler)(BOOL success);
#property (nonatomic, copy) void (^MYCompletionHandler)(bool *) completion;
- (void)loginUserWithCompletion:(MYCompletionHandler)completion {
self.completion = completion;
// start your login processing here
}
// when the login response is received
self. completion(##DID_IT_WORK##);
Your current code doesn't work because the main runloop isn't running while your hard loop is running, so the delegate methods are queued waiting to be processed. Don't try to work around it with the run loop, deal with the asynchronous nature properly.
The didReceiveData method will be called when the data has been received. If you need to process the data returned, do it in the delegate method.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// process your data in here
}
The logIn function has to finish before didReceiveData will be called. You cannot block for a response. That's the nature of NSURLConnection.

Assigning data from an anonymous handler ios

I'm trying to get a reference of an NSArray that gets passed when I call sendAsyncrhonousRequest. Once I have that NSArray, I'd like to assign it to a class attribute but it seems I can't do that.
#implementation BarTableViewController {
NSArray *_jsonArray;
}
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode == 200 && data.length > 0 && error == nil)
{
NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];
if (!jsonArray) {
NSLog(#"Error parsing JSON: %#", e);
} else {
_jsonArray = jsonArray; // this doesn't work? _jsonArray is at the class level
}
}
else if (error)
{
NSLog(#"HTTP Status: %ld", (long)statusCode);
}
else if (statusCode != 200)
{
NSLog(#"HTTP Status: %ld", (long)statusCode);
}
}];
If I traverse jsonArray it will correctly display the data. If I assign it to _jsonArray to use it later, it no longer returns any data. The count of the array is zero.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _jsonArray.count; // always returns zero
}
How can I assign jsonArray to a class attribute so that I can use that data later?
My theory was correct. The UI was loading before the request finished. What I did to fix this issue was to call
[self.tableView reloadData];
Inside the async request.
Forget simple assignment:
#implementation BarTableViewController {
// NSArray *_jsonArray; forget it
}
instead own the object.
#interface BarTableViewController()
#property(nonatomic, strong)NSArray *jsonArray;
#end
/* --------- */
#implementation BarTableViewController
#syntethise jsonArray = _jsonArray;
#end
then to make it own it
self.jsonArray = jsonArray; // will call synthesized setter
I'm assuming you are using ARC, because you get a nil value instead of a dangling pointer. Now you will get a valid jsonArray.

AFNetworking Synchronous Operation in NSOperationQueue on iPhone

My app is working this way :
- create an album and take pictures
- send them on my server
- get an answer / complementary information after picture analysis.
I have some issue with the sending part. Here is the code
#interface SyncAgent : NSObject <SyncTaskDelegate>
#property NSOperationQueue* _queue;
-(void)launchTasks;
#end
#implementation SyncAgent
#synthesize _queue;
- (id)init
{
self = [super init];
if (self) {
self._queue = [[NSOperationQueue alloc] init];
[self._queue setMaxConcurrentOperationCount:1];
}
return self;
}
-(void) launchTasks {
NSMutableArray *tasks = [DataBase getPendingTasks];
for(Task *t in tasks) {
[self._queue addOperation:[[SyncTask alloc] initWithTask:t]];
}
}
#end
and the SyncTask :
#interface SyncTask : NSOperation
#property (strong, atomic) Task *_task;
-(id)initWithTask:(Task *)task;
-(void)mainNewID;
-(void)mainUploadNextPhoto:(NSNumber*)photoSetID;
#end
#implementation SyncTask
#synthesize _task;
-(id)initWithTask:(Task *)task {
if(self = [super init]) {
self._task = task;
}
return self;
}
-(void)main {
NSLog(#"Starting task : %#", [self._task description]);
// checking if everything is ready, sending delegates a message etc
[self mainNewID];
}
-(void)mainNewID {
__block SyncTask *safeSelf = self;
[[WebAPI sharedClient] createNewPhotoSet withErrorBlock:^{
NSLog(#"PhotoSet creation : error")
} andSuccessBlock:^(NSNumber *photoSetID) {
NSLog(#"Photoset creation : id is %d", [photoSetID intValue]);
[safeSelf mainUploadNextPhoto:photoSetID];
}];
}
-(void)mainUploadNextPhoto:(NSNumber*) photoSetID {
//just admit we have it. won't explain here how it's done
NSString *photoPath;
__block SyncTask *safeSelf = self;
[[WebAPI sharedClient] uploadToPhotosetID:photoSetID withPhotoPath:photoPath andErrorBlock:^(NSString *photoPath) {
NSLog(#"Photo upload error : %#", photoPath);
} andSuccessBlock:^(NSString *photoPath) {
NSLog(#"Photo upload ok : %#", photoPath);
//then we delete the file
[safeSelf mainUploadNextPhoto:photoSetID];
}];
}
#end
Every network operations are done using AFNetworking this way :
-(void)myDummyDownload:(void (^)(NSData * data))successBlock
{
AFHTTPClient* _httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:#"http://www.google.com/"]];
[_httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
NSMutableURLRequest *request = [_httpClient requestWithMethod:#"GET" path:#"/" nil];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
AFHTTPRequestOperation *operation = [_httpClient HTTPRequestOperationWithRequest:(NSURLRequest *)request
success:^(AFHTTPRequestOperation *operation, id data) {
if(dataBlock)
dataBlock(data);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Cannot download : %#", error);
}];
[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
NSLog(#"Request time out");
}];
[_httpClient enqueueHTTPRequestOperation:operation];
}
My problem is : my connections are made asynchronously, so every task are launched together without waiting fo the previous to finish even with [self._queue setMaxConcurrentOperationCount:1] in SyncAgent.
Do I need to perform every connection synchronously ? I don't think this is a good idea, because a connection never should be done this way and also because I might use these methods elsewhere and need them to be performed in background, but I cannot find a better way. Any idea ?
Oh and if there is any error/typo in my code, I can assure you it appeared when I tried to summarize it before pasting it, it is working without any problem as of now.
Thanks !
PS: Sorry for the long pastes I couldn't figure out a better way to explain the problem.
EDIT: I found that using a semaphore is easier to set up and to understand : How do I wait for an asynchronously dispatched block to finish?
If you have any control over the server at all, you should really consider creating an API that allows you to upload photos in an arbitrary order, so as to support multiple simultaneous uploads (which can be quite a bit faster for large batches).
But if you must do things synchronized, the easiest way is probably to enqueue new requests in the completion block of the requests. i.e.
// If [operations length] == 1, just enqueue it and skip all of this
NSEnumerator *enumerator = [operations reverseObjectEnumerator];
AFHTTPRequestOperation *currentOperation = nil;
AFHTTPRequestOperation *nextOperation = [enumerator nextObject];
while (nextOperation != nil && (currentOperation = [enumerator nextObject])) {
currentOperation.completionBlock = ^{
[client enqueueHTTPRequestOperation:nextOperation];
}
nextOperation = currentOperation;
}
[client enqueueHTTPRequestOperation:currentOperation];
The following code works for me, but I am not sure of the drawbacks. Take it with a pinch of salt.
- (void) main {
NSCondition* condition = [[NSCondition alloc] init];
__block bool hasData = false;
[condition lock];
[[WebAPI sharedClient] postPath:#"url"
parameters:queryParams
success:^(AFHTTPRequestOperation *operation, id JSON) {
//success code
[condition lock];
hasData = true;
[condition signal];
[condition unlock];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//failure code
[condition lock];
hasData = true;
[condition signal];
[condition unlock];
}];
while (!hasData) {
[condition wait];
}
[condition unlock];
}

Sudz-C Objective-C WebServices logging valid results, but always returning NIL delegate object

I am using SudzC to create a WebService Client, and after some attempts of dealing with configuration, etc., I was able to return data. When I turn on logging within the service, the response logs, as seen below. (I have added spaces to all of the XML tags to enable them to print within StackOverflow. Please accept that the XML works because I have an existing Java application that retrieves this data into JAXB Objects and currently works.)
2012-03-13 11:21:43.936 SampleProject[976:f803]
< SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" >
< SOAP-ENV:Header/ >
< SOAP-ENV:Body >
< ns2:GetDevicesResponse xmlns:ns2="http://somesite.com/cayman/schemas" >
< ns2:Devices>
< ns2:DeviceName>Neelam 65< /ns2:DeviceName>
< ns2:DeviceName>Neelam 66< /ns2:DeviceName>
< /ns2:Devices>
< /ns2:GetDevicesResponse>
< /SOAP-ENV:Body>
< /SOAP-ENV:Envelope>
However, when I attempt to use a delegate, OR
[service GetDevices:self action:#selector(handleFind:)];
the value in "result" is alway nil.
Any ideas?
XpressViewViewController.h
#interface XpressViewViewController : UIViewController <SoapDelegate>
...
XpressViewViewController.m
#implementation XpressViewViewController
...
- (IBAction)initiateService:(id)sender {
CaymanService* service = [[CaymanService alloc]init];
service.logging = YES;
NSMutableDictionary * headers = [[NSMutableDictionary alloc]init];
SoapLiteral *soapLiteral =[SoapLiteral literalWithString: #"<wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><wsse:UsernameToken xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"><wsse:Username>DUMMY</wsse:Username><wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">test</wsse:Password></wsse:UsernameToken></wsse:Security>"];
[headers setValue:soapLiteral forKey:#"header"];
[service setHeaders: headers];
[service setNamespace:#"http://company.com/cayman/schemas"];
//SoapRequest *sr = [service GetDevices:self action:#selector(handleFind:)];
//NSLog(#"Request: ", sr.description);
[service GetDevices:self];
}
- (void) onload: (id) result;
{
//Why are you ALWAYS nil...What am I missing???
NSLog(#"Data: %#", result);
}
- (void) onerror: (NSError*) error;
{
NSLog(#"Error: %#", error);
}
- (void) onfault: (SoapFault*) fault;
{
NSLog(#"Fault: %#", fault);
}
-(void)handleFind: (id) result {
if([result isKindOfClass: [NSError class]]) {
NSLog(#"Error: %#",result);
return;
}
if([result isKindOfClass: [SoapFault class]]) {
NSLog(#"Error: %#",result);
return;
}
// This doesn't matter because result is ALWAYS nil
NSArray *data = [[NSArray alloc ]initWithArray: result];
NSLog(#"We have %# devices ", [NSNumber numberWithInt:data.count]);
}
I was having a similar problem, and the solution outlined at this link helped:
http://code.google.com/p/sudzc/issues/detail?id=40
Basically - there seems to be a bug in SudzC that hasn't been patched yet, but a one-line change to the Soap.m file works around it.