2012-03-19 11 views
0

私は、アプリケーションのCoreDataエンティティからのデータを表示するいくつかのビューを持っています。ビューに必要なデータを取得するには、しばしば間違っている複数のfetchRequestsを実装する必要があります。多分、SQLデータベースの意味でCoreDataを考えるような基本的なミスを犯しているのかもしれません。複雑な述語を持つ複数のフェッチ要求

実際、私の意見の1つとして、22のフェッチ要求がありますが、これは私が必要とするものを達成する正しい方法かもしれませんが、iPhone/Objective-Cの初心者として私のアプローチに疑問を呈することはできません。ここでは、多くのfetchRequestのうちの2つを示しているコードのスニペットがあります。間違っていると正しい方向に微妙な変化がありますか?あなたが提供することができるかもしれないどのような援助、リンクや方向を事前に

 SGK_T4T_01AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
    NSManagedObjectContext *context = [appDelegate managedObjectContext]; 
    NSEntityDescription *entityDiscription = [NSEntityDescription entityForName:@"Sessions" inManagedObjectContext:context]; 

//Swim 3 Count 
     NSFetchRequest *request2 = [[NSFetchRequest alloc] init]; 
     [request2 setEntity:entityDiscription]; 
     [request2 setResultType:NSDictionaryResultType]; 
     NSExpression *keyPathExpression2 = [NSExpression expressionForKeyPath:@"sport"]; 
     NSExpression *swimCountExpression = [NSExpression expressionForFunction:@"count:" arguments:[NSArray arrayWithObject:keyPathExpression2]]; 
     NSExpressionDescription *expressionDescription2 = [[NSExpressionDescription alloc] init]; 
     [expressionDescription2 setName:@"swimCount"]; 
     [expressionDescription2 setExpression:swimCountExpression]; 
     [expressionDescription2 setExpressionResultType:NSInteger16AttributeType]; 
     [request2 setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription2]]; 
     NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"(date >= %@ AND sport like %@)", swimSinceDateAsDate, sportTypeSwim]; 
     [request2 setPredicate:pred2]; 

     NSError *error2; 
     NSArray *objects2 = [context executeFetchRequest:request2 error:&error2]; 
     if (objects2 == nil) { 
      NSLog(@"The fetch request returned an array == nil"); 
     } else { 
      _swimTotalSwimCountLabel.text = [[NSString alloc] initWithFormat:@"%@", [[objects2 objectAtIndex:0] valueForKey:@"swimCount"]]; 
     } 

     //Swim 4 Fastest 1500m Time Trial 
     NSFetchRequest *request3 = [[NSFetchRequest alloc] init]; 
     [request3 setEntity:entityDiscription]; 
     [request3 setResultType:NSDictionaryResultType]; 
     NSExpression *keyPathExpression3 = [NSExpression expressionForKeyPath:@"time1"]; 
     NSExpression *swimfastest1500Expression = [NSExpression expressionForFunction:@"min:" arguments:[NSArray arrayWithObject:keyPathExpression3]]; 
     NSExpressionDescription *expressionDescription3 = [[NSExpressionDescription alloc] init]; 
     [expressionDescription3 setName:@"swimFastest1500"]; 
     [expressionDescription3 setExpression:swimfastest1500Expression]; 
     [expressionDescription3 setExpressionResultType:NSInteger16AttributeType]; 
     [request3 setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription3]]; 
     NSString *sessType3 = @"Time Trial - 1500m"; 
     NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"(date >= %@ AND sport like %@ AND sessiontype like %@)", swimSinceDateAsDate, sportTypeSwim, sessType3]; 
     [request3 setPredicate:pred3]; 

     NSError *error3; 
     NSArray *objects3 = [context executeFetchRequest:request3 error:&error3]; 
     if (objects3 == nil) { 
      NSLog(@"The fetch request returned an array == nil"); 
     } else { 
      NSUInteger durationInSeconds = [[[objects3 objectAtIndex:0] valueForKey:@"swimFastest1500"] integerValue]; 
      NSUInteger durationInMinutes = durationInSeconds/60; 
      NSUInteger durationRemainder = durationInSeconds % 60; 
      _swimTt1500PaceLabel.text = [[NSString alloc] initWithFormat:@"%02i:%02i", durationInMinutes, durationRemainder]; 
     } 

おかげで...

答えて

0

述語のものがあまりにも悪くはありません - しかし、あなたがに制限している理由は、私が求めることができますこれらすべてのNSExpressionDescriptions?分析して、具体的なパフォーマンス改善が見出されたプロパティだけに限定することでそれを見つけましたか?コアデータは、通常、メモリのフォールディング/最適化にはかなり優れており、ステートメントをかなりクリーンアップする可能性があります。

+0

あなたの答えに感謝します - あなたの質問に答えて、私は私のアプリを分析したときに、それはエラーなしで、すべて緑色でした。私はあなたの声明「すべてのNSExDescに限定」を守っているかどうかはわかりません。まだ初心者の方が多すぎますが、私は自分のコードを整理する方法を常に探しています。だからこそ精巧にする必要があるなら、非常に感謝しています。 – Sean

+0

私たちはすべてのビルド(遅いビルドだけど、技術的な借金は忍び込んでいない:)のためにそれを反転します。実際にはNSExpressionは非常にクールです(より高いパフォーマンスを実現するように見えますが)が冗長です。私たちはかなりの数のエンティティ/フェッチを持ち、パフォーマンスチェックを実行していますが、それらをまだ使用していません。それらのコードを削除することはできますが、パフォーマンスは低下する可能性があります。インストゥルメントを使用する前に、追加のラインが追加されてどれくらい増えているかを知る必要があります。 :) –

+0

スコットに返事する時間をとってくれてありがとう、私はあなたの推薦を見てみましょう... – Sean

関連する問題