I’m developing an iOS 4 app for iPad with latest SDK and XCode 4.2.
I have a problem with a JSON web service. I’m using a timer to check every 3 seconds if some data has changed. The problem is that data changes, but I can’t see that changes on my app.
Data received asynchronously is always the same. It is like NSURLConnection has a cache or something like that, and it always returning the first data it takes.
Here is my code:
ControlPanelJSON.h
#import <Foundation/Foundation.h>
#import "SBJson.h"
#import "WebServiceDelegate.h"
@interface ControlPanelJSON : NSObject
{
SBJsonParser* parser;
NSURLRequest* request;
NSMutableData* receivedData;
BOOL isEncuestaVisible;
BOOL isInfoVisible;
BOOL isTwitterVisible;
id<WebServiceDelegate> delegate;
}
@property (nonatomic, readonly) BOOL isEncuestaVisible;
@property (nonatomic, readonly) BOOL isInfoVisible;
@property (nonatomic, readonly) BOOL isTwitterVisible;
- (id) initWithWebServiceURLString:(NSString*)webServiceURL
delegate:(id<WebServiceDelegate>)del;
- (void)callWebService;
@end
ControlPanelJSON.m
#import "ControlPanelJSON.h"
#define kRootKey @"s"
#define kControlKey @"c"
#define kEstadoKey @"e"
#define kEncuestaTitle @"[ zzz ]"
#define kInfoTitle @"[ yyy ]"
#define kTwitterTitle @"[ xxx ]"
@implementation ControlPanelJSON
@synthesize isEncuestaVisible;
@synthesize isInfoVisible;
@synthesize isTwitterVisible;
- (id) initWithWebServiceURLString:(NSString*)webServiceURL
delegate:(id<WebServiceDelegate>)del
{
if (self = [super init])
{
delegate = [del retain];
request = [[NSURLRequest alloc] initWithURL: [NSURL URLWithString: webServiceURL]
cachePolicy: NSURLCacheStorageNotAllowed
timeoutInterval: 60.0];
parser = [[SBJsonParser alloc] init];
}
return self;
}
- (void) dealloc
{
[parser release];
[request release];
[receivedData release];
[super dealloc];
}
- (void)callWebService
{
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection)
{
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
}
else
{
[delegate errorReceivedWithMessage:NSLocalizedString(@"CONNERROR", nil)];
}
}
#pragma mark -
#pragma mark - NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.
// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// Cuando se recibe este mensaje se debe descartar todo lo recibido anteriormente.
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
// receivedData is declared as a method instance elsewhere
[receivedData release];
// inform the user
[delegate errorReceivedWithMessage:
[NSString stringWithFormat:@"Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]]];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *json_string = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSDictionary* datos = [parser objectWithString:json_string error:nil]; // TODO: Otro error que manejar
[connection release];
//[receivedData release];
//[parser release];
[json_string release];
NSArray* data = [datos objectForKey:kRootKey];
for (int i = 0; i < data.count; i++)
{
NSDictionary* object = [data objectAtIndex:i];
NSString* controlName = [object objectForKey:kControlKey];
NSString* controlEstado = [object objectForKey:kEstadoKey];
if ([controlName isEqualToString: kEncuestaTitle])
{
isEncuestaVisible = ([controlEstado isEqualToString: @"1"]);
continue;
}
else if ([controlName isEqualToString: kInfoTitle])
{
isInfoVisible = ([controlEstado isEqualToString: @"1"]);
continue;
}
else if ([controlName isEqualToString: kTwitterTitle])
{
isTwitterVisible = ([controlEstado isEqualToString: @"1"]);
continue;
}
}
[delegate dataReceived];
}
@end
After this code line:
NSString *json_string = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
I added a NSLog, and I always getting the same json_string
{“ctrls”: [ { “control”: “[ xxx ]”,”estado”: “0” },{ “control”: “[ yyy ]”,”estado”: “0” },{ “control”: “[ zzz ]”,”estado”: “0” } ] }
Any clue?
Try changing the cache policy when you create your
NSURLRequestto: