0

私は 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;
}
4

1 に答える 1

0

私の知る限り、コア データは非常に高速です。WWDC コア データのベスト プラクティス ビデオでは、これに関する情報や、計測器を使用して速度を確認する方法などを提供しています。

于 2013-07-28T04:40:19.613 に答える