リソースのダウンロードに関して、AFNetworkingを使用してキューに入れることができます。
AFHTTPClientの - (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)操作を使用することができます。
まずそのようなあなた自身のAFHTTPClientを保持するシングルトンを作成します。
@interface CustomHTTPClient : NSObject
+ (AFHTTPClient *)sharedHTTPClient;
@end
@implementation CustomHTTPClient
+(AFHTTPClient *)sharedHTTPClient {
static AFHTTPClient *sharedHTTPClient = nil;
if(sharedHTTPClient == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Create the http client
sharedHTTPClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://mybaseurl.com"]];
});
}
return sharedHTTPClient;
}
@end
は、そのようにのようなあなたの要求をキュー:
// Store the operations in case the failure block needs to cancel them
__block NSMutableArray *operations = [NSMutableArray array];
// Add operations for url
for (NSURL *url in urls) {
NSURLRequest *request = [NSURLRequest requestWithURL:url];
__block AFHTTPRequestOperation *operation = [[CustomHTTPClient sharedHTTPClient]
HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation , id responseObject){
// Do something
}
failure:^(AFHTTPRequestOperation *operation , NSError *error){
// Cancel all operations if you need to
for (AFHTTPRequestOperation* operation in operations) {
[operation cancel];
}
}];
[operations addObject:operation];
}
for (AFHTTPRequestOperation* operation in operations) {
[[CustomHTTPClient sharedHTTPClient] enqueueHTTPRequestOperation:operation];
}
enqueueBatchOfHTTPRequestOperationsもあります:あなたがする必要がある場合:progressBlock:completionBlockは進行状況を監視する。
AFNetworkingプロジェクト: https://github.com/AFNetworking/AFNetworking/
AFNetworkingドキュメント: http://afnetworking.org/Documentation/index.html
あなたがバックグラウンドスレッド(と同様に、それぞれ他のスレッド)に別々の 'NSManagedObjectContext'を持っている必要があります。ネットワークからオブジェクトを受け取った後、それらをあなたのバックグラウンド管理されたオブジェクトコンテキストに入れて、そのコンテキストをあなたのアプリ内の他の管理オブジェクトコンテキストに同期させます。 – nielsbot
セルネットワークのネットワークを理解することは、レイテンシが最大の問題であることです。接続ごとに時間コストを支払うことになります。要求を結合することができれば、より良いネットワークパフォーマンスを得ることができます。 – nielsbot