gpt4 book ai didi

ios - NSURLConnection 有缓存吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:16:33 25 4
gpt4 key购买 nike

我正在使用最新的 SDK 和 XCode 4.2 为 iPad 开发 iOS 4 应用。

我的 JSON 网络服务有问题。如果某些数据已更改,我正在使用计时器每 3 秒检查一次。问题是数据发生了变化,但我在我的应用程序中看不到这些变化。

异步接收的数据总是相同的。这就像 NSURLConnection 有一个 cache 或类似的东西,它总是返回它获取的第一个数据。

这是我的代码:

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”}]}

有什么线索吗?

最佳答案

在创建 NSURLRequest 时尝试将缓存策略更改为:

NSURLRequestReloadIgnoringLocalAndRemoteCacheData

关于ios - NSURLConnection 有缓存吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9667414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com