0

My code:

NSMutableArray *sstr = [[DBModel database]getCName];
NSArray *aarr = [[sstr objectAtIndex:0] componentsSeparatedByString:@"!"];

acName = [aarr objectAtIndex:0];
acMobileno = [aarr objectAtIndex:1];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http:///userstatus.php?name=%@&mobileno=%@&status=off",[aarr objectAtIndex:0],[aarr objectAtIndex:1]]];

NSMutableURLRequest *postRequest = [[NSMutableURLRequest alloc]initWithURL:url];
[postRequest setHTTPMethod:@"POST"];

NSString *stringBoundary = @"------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postBody = [NSMutableData data];

//name
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"name\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%@",[aarr objectAtIndex:0]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
//mobileno
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"mobileno\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%@",[aarr objectAtIndex:1]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
//status
NSString *status = @"off";
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"status\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%@",status] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

[postRequest setHTTPBody:postBody];

NSData *returndata=[NSURLConnection sendSynchronousRequest:postRequest returningResponse:nil error:nil];

NSString *string1=[[NSString alloc]initWithData:returndata encoding:NSUTF8StringEncoding];

NSLog(@"OFF Status report = %@",string1);

I am running this code with performSelectorInBackground: in didEnterBackground method.

Sometimes it gives me the strange problem of not updating the table in server.

Is it problem with using performSelector in delegate method or should i change this request to asynchronous?

Nate
  • 31,017
  • 13
  • 83
  • 207
Xcode
  • 455
  • 2
  • 5
  • 16
  • UI Changes are best done on the main thread. Once you are done with the actions in the Background thread, call the Main Thread again using `- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait` and update the table.. – Roshit May 30 '13 at 04:47
  • Right.. My bad.. I read **updating the table** instead of **updating the table in server** – Roshit May 30 '13 at 04:50

1 Answers1

1

You appear to be starting a network request when your app goes to the background. This means, for example, that when the user closes the app, you begin making a network transaction. That's not the way Apple intends for apps to work (ideally).

From the documentation on applicationDidEnterBackground: (bold is mine):

Your implementation of this method has approximately five seconds to perform any tasks and return. If you need additional time to perform any final tasks, you can request additional execution time from the system by calling beginBackgroundTaskWithExpirationHandler:. In practice, you should return from applicationDidEnterBackground: as quickly as possible. If the method does not return before time runs out your application is terminated and purged from memory.

So, you're probably seeing that your network transaction is not always finishing quickly enough.

I'd recommend rethinking your app. applicationDidEnterBackground: is probably not the right time to do this work. If you really need to do some work in the background, see this example of using a background task. If you put your code in a background task, then you should be able to use NSURLConnection either with a synchronous, or asynchronous request.

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • What method are you talking about? I only know the `applicationDidEnterBackground:` method. There is `applicationWillEnterForeground:`, but that's for when the user is **opening** your app again. – Nate May 30 '13 at 04:52
  • Did you mean `applicationWillResignActive:`? If so, I would still say, "no". If you really need to contact your server when the user quits the app, follow the example I linked to showing how to begin a "background task with expiration handler". – Nate May 30 '13 at 04:56
  • I added some code in didBecomeActiveMethod to perform some updation in server.Is there possibility of getting same error? – Xcode May 30 '13 at 05:10
  • No. If you put your code in `applicationDidBecomeActive:`, then you will not have the request fail because it doesn't complete within 5 seconds. Of course, there might be a **different** problem. You just have to try it and see what happens. – Nate May 30 '13 at 07:35