【发布时间】:2012-03-28 20:56:59
【问题描述】:
我正在使用最新的 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" },{ “控制”:“[zzz]”,“estado”:“0”}]}
有什么线索吗?
【问题讨论】:
-
您可以更改缓存策略并玩弄其他东西,但您仍然可以通过在网络中缓存来获得比特。唯一合理的万无一失的解决方案是在您的 URL 中添加一个虚假参数,每次请求都会增加该参数。
标签: ios json caching nsurlconnection