2013-07-28 61 views
0

我一直在避免使用IOS核心數據模型,現在有一個效率問題。如果我想經常檢查我的表的數量,並隨機查看我的應用程序的部分。我想知道如果爲這種「計數」方法創建上下文,實體和提取請求,就像這樣,是輕量級或昂貴的。如果價格昂貴,我可以將哪些屬性設置爲屬性並創建一次或者創建爲單例等。我擔心數據模型的動態特性可能需要新的工作對象。創建IOS核心數據模型對象是否昂貴?

-(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; 
} 

回答

0

據我所知核心數據非常快,WWDC核心數據的最佳做法的視頻提供這方面的信息,以及如何使用工具來檢查速度等