Projemde web'den veri indirmek için AFNetworking kullanıyorum. Kullanıcı önbelleğe alınmış verileri sunmak için NSURLRequest
üzerinde NSURLRequestUseProtocolCachePolicy
'dan yararlanıyorum (önbellek geçerli ise).AFNetworking ve yanıt önbellek kullanımı
İstek yöntemi: Yeni mantığına geçiş istiyoruz
// create NSURL request
NSURLRequest *request = [ServerFactory URLGETRequestWithURL:url];
//creating AFHTTPRequestOperation
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
//set serializaer
operation.responseSerializer = [AFJSONResponseSerializer serializer];
//need to specify that text is acceptable content type, otherwise the error occurs
operation.responseSerializer.acceptableContentTypes = [MyRepository acceptableContentTypes];
//running fetch request async
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//parse data
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//error handling
}];
//start async operation
[operation start];
Kabul edilebilir içerik türleri yöntem
+ (NSSet *)acceptableContentTypes
{
return [NSSet setWithObjects:@"application/json", @"text/plain", @"text/html" ,nil];
}
ServerFactory olsun yöntemler Şimdi
+ (NSURLRequest *)URLGETRequestWithURL:(NSString *)URL
{
NSMutableURLRequest *request = [[ServerFactory URLRequestWithURL:URL] mutableCopy];
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
[request setHTTPMethod:@"GET"];
return request;
}
+ (NSURLRequest *)URLRequestWithURL:(NSString *)URL
{
// creating NSURL to give to NSURLRequest
NSURL *theURL = [NSURL URLWithString:URL];
//adding service version in http header
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:theURL];
[request addValue:HTTP_HEADER_VERSION_VALUE forHTTPHeaderField:HTTP_HEADER_VERSION_NAME];
//returing request
return request;
}
Bu benim kodudur:
- verileri önbelleğe alın
- Eğer önbelleğe alınan veriler geçerli
- önbelleğe alınmış verileri ile If-Modified-Since alınan önbelleğe alınmış veriler zaman damgasıyla
- Sunucu respondes 304 olarak ayarlanmış başlık Değiştirilmedi ile
- Sevk Yeni istek kullanıcı Serve yeni verilerle yeni verileri
- Güncelleme UI varsa önbellek hala Tamam, ya da 200 OK ise
- Eğer önbellek veri ex
- pired Böylece temelde önbelleğe alınmış verileri hizmet ama benim önbelleğe alınan veriler hala sunucuda geçerli olduğunu ya da olmadığını yeni veri indirmek için olmadığını kontrol etmek istiyorum
web yeni veriler alın. bunu arşivlemenin bir yolu var mı? AFHTTPRequestOperation
numaralı telefondan setCacheResponseBlock
ile denedim ancak önbelleğe alınmış veri zaman damgasını alamıyorum. Bunu yapmak için "daha akıllı" bir yolu var mı?
AFNetworking 2.0 kullanıyorum – paxx
Bu konuyu inceleyin, size yardımcı olabilir: http://stackoverflow.com/a/21556002/514181 – Darrarski