ユーザーが初めてアプリを開くときは、大量のデータをダウンロードする必要があります。このすべてのデータをサーバーからJSON形式で取得します。ユーザーに応じて、これらのJSONファイルはそれぞれ10kb〜30mbの範囲であり、10個以上あります。
JSONのレコード数が500程度であれば問題ありませんが、前述のように、10,000以上のレコードがあり、最大30MBのサイズになる場合があります。
より大きなJSONをダウンロードすると、最終的にメモリの警告が表示されてアプリが起動するまで、アプリは大量のメモリを割り当てます。
CFDataは、didReceiveDataで構築しているNSMutableDataである必要があるようです。単一のJSONをダウンロードすると、CFData(ストア)が上昇します。解析を開始すると、上昇が停止します。
次のJSONをダウンロードして解析する前に、そのデータをクリアするにはどうすればよいですか?
以下に示すように、200MBのCFData(ストア)がメモリ内にあります。
-
CFDataを掘り下げても、私を助けることはあまりわかりません。
これらのさまざまなJSONを取得するための操作を作成するコードは次のとおりです--
- (void)checkForUpdates
{
if(!_globals)
_globals = [MySingleton sharedInstance];
MyAFHTTPClient* client = [MyAFHTTPClient sharedClient];
NSString* path = [NSString stringWithFormat:@"cache?deviceUID=%@&token=%@",[_globals getMacAddress], [_globals getToken]];
NSURLRequest* request = [client requestWithMethod:@"GET" path:path parameters:nil];
_currentEnvironment = [_globals getActiveEnvironment];
if(!_currentEnvironment.didDownloadDataValue)
[self setupAndShowHUDinView:self.view withText:@"Checking for updates..."];
AFJSONRequestOperation* operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
for(NSString *str in [JSON valueForKey:@"Views"])
{
//for each table i need to update, create a request to
NSString* path = [NSString stringWithFormat:@"cache/%@/?deviceUID=%@&token=%@", str, @"00000-00000-0000-00001", [_globals getToken]];
NSURLRequest* request = [client requestWithMethod:@"GET" path:path parameters:nil];
AFJSONRequestOperation* operation2 = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
// even if this is comment this out and I do nothing but download, app crashes
//[self updateTable:str withJSON:JSON];
} failure:nil];
[operation2 setSuccessCallbackQueue:backgroundQueue];
[client.operationQueue addOperation:operation2];
numRequests++;
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}];
[operation start];
}
そのCFDataはメモリ内にある私のJSON応答ですか?運が悪かったのでNSURLCacheをクリアしようとしました--
また、JSONストリーミングについても少し調べました。これは、メモリ内のオブジェクトの量を減らすのに役立ちますか?
それに加えて、私が考えることができる他の唯一のオプションは、ある種のページングを実装することです。しかし、ユーザーがすべてのデータを持っている必要があります。
どんな助け/提案も大歓迎です!ありがとう!
-
編集
OK、AFNetworkingを削除して、ネイティブ関数を使用することにしました。これを行うことで、CFDataの同じビルドを取得します。NSOperationのこのサブクラスを使用しており、次のように操作を作成/追加しています。
NSOperationQueue *operationQueue;
operationQueue = [[NSOperationQueue alloc]init];
[operationQueue setMaxConcurrentOperationCount:1];
for(NSString *str in [views valueForKey:@"Views"])
{
NSString* path = [NSString stringWithFormat:@"%@cache/%@/?deviceUID=%@&token=%@", _globals.baseURL, str, @"00000-00000-0000-00001", [_globals getToken]];
LibSyncOperation *libSyncOperation = [[LibSyncOperation alloc] initWithURL:path];
[operationQueue addOperation:libSyncOperation];
}