私は IOS コア データ モデルの使用を避けてきましたが、今では効率性に問題があります。テーブルのカウントを頻繁に、アプリのランダムな部分でチェックしたい場合。この「count」メソッドのコンテキスト、エンティティ、およびフェッチ リクエストの作成が、このように軽量なのか高価なのかを知りたいです。高価な場合は、どれをプロパティとして設定して一度だけ作成するか、シングルトンとして作成するかなどを選択できます。データモデルの動的な性質により、新しい作業オブジェクトが必要になるのではないかと心配しています。
-(NSInteger) count {
NSManagedObjectContext *context = [[MdCoreDataModel sharedInstance] contextFortune];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName: @"MyTable" inManagedObjectContext: context];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entityDescription];
NSError *error = nil;
NSUInteger countTotal = [context countForFetchRequest: fetchRequest error: &error];
if (error) {
NSString *stringErrorMsg = [NSString stringWithFormat:@"%@\n%@", [error localizedDescription], [error localizedFailureReason]];
NSLog(@"%s %@", __FUNCTION__, stringErrorMsg);
countTotal = 0;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Debug" message: stringErrorMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alertView show];
}
return countTotal;
}