我正在使用最新的SDK和XCode 4.2为iPad开发iOS 4应用程序。
我遇到了JSON Web服务的问题。如果某些数据发生了变化,我每隔3秒就会使用一个计时器进行检查。问题是数据发生了变化,但我无法看到我的应用程序发生的变化。
异步接收的数据始终相同。就像NSURLConnection
有一个缓存之类的东西,它总是返回它所需的第一个数据。
这是我的代码:
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
在此代码行之后:
NSString *json_string = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
我添加了一个NSLog,我总是得到相同的json_string
{“ctrls”:[{“control”:“[xxx]”,“estado”:“0”},{“control”:“[yyy]”,“estado”:“0”},{ “control”:“[zzz]”,“estado”:“0”}}}
有任何线索吗?
答案 0 :(得分:2)
尝试在创建NSURLRequest
时更改缓存政策:
NSURLRequestReloadIgnoringLocalAndRemoteCacheData
答案 1 :(得分:1)
请使用:
NSURLRequestReloadIgnoringCacheData
请注意NSURLCacheStorageNotAllowed
为NSCachedURLResponse
课程,而不是NSURLRequest
。
根据NSURLRequest.h
标头文件,NSURLRequestReloadIgnoringLocalAndRemoteCacheData
未实现,如下所示。
enum
{
NSURLRequestUseProtocolCachePolicy = 0,
NSURLRequestReloadIgnoringLocalCacheData = 1,
NSURLRequestReloadIgnoringLocalAndRemoteCacheData = 4, // Unimplemented
NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData,
NSURLRequestReturnCacheDataElseLoad = 2,
NSURLRequestReturnCacheDataDontLoad = 3,
NSURLRequestReloadRevalidatingCacheData = 5, // Unimplemented
};
typedef NSUInteger NSURLRequestCachePolicy;