2011-10-23 13 views
3

コアデータに格納されたコアデータをフェッチしようとしていますが、eventStartDateというフィールドの日付範囲にフィルタリングされました。 Xcodeでコアデータ項目をプロキシします。フェッチ要求を実行すると、何も返されません。私は、SQLのログを見てみましたし、フォローのSQLが呼び出されていることがわかり、正しい見ている:フェッチ述語としての日付の範囲 - コアデータ間のNSDate述語

SELECT 0, t0.Z_PK FROM ZEVENT t0 WHERE (t0.ZEVENTSTARTDATE > ? AND t0.ZEVENTSTARTDATE <= ?) ORDER BY t0.ZEVENTSTARTDATE 

私がPOにしようとしたとき、私は、戻って何を取得[mgtEventArrayカウント]私はオブジェクト上nilを取得します。

フェッチのための私のコード:

NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:[NSDate date]]; 
NSDate *startDate = [calendar dateFromComponents:components]; 
[components setMonth:10]; 
[components setDay:0]; 
[components setYear:0]; 
NSDate *endDate = [calendar dateByAddingComponents:components toDate:startDate options:0]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"((eventStartDate > %@) AND (eventStartDate <= %@))",startDate,endDate]; 


NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext]; 
[fetchRequest setEntity:entity]; 
[fetchRequest setFetchBatchSize:20]; 

[fetchRequest setPredicate:predicate]; 

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortDescKey ascending:YES]; 
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 
[sortDescriptor release]; 

NSError *error = nil; 
NSArray *mgtEventArray = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
if(error != nil) 
    NSLog(@"%@", error); 
return mgtEventArray; 

答えて

3

これはあなたの変数startDateendDateのように、予期しない値が含まれているようです。 startDateは日なしなどの日付コンポーネントであり、のendDateです。

したがって、これらの値を確認するには、述語定義の前にブレークポイントまたはNSLogステートメントを置くことをお勧めします。

また、あなたが使用しているのは何ですか?NSCalendardocumentationから:

日、週、曜日、月、年の数字は、一般的に1ベースですが、 は、カレンダー固有の例外があるかもしれません。

dayプロパティを0に設定すると問題が発生する可能性があります。

2

があなたの述語のための別のフォーマットを試してみてください。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(eventStartDate > %@) AND (eventStartDate <= %@)",startDate,endDate];` 

は、単一の文の周りではなく、文全体の周りparathesesを置きます。

+3

-1カッコで間違っても違いはありません –

+0

実際には、現在では機能しています。 –

0

iOS 6以降、前述のように、この述語が機能します。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startDate > %@",[NSDate date];` 

ありがとうございます。

関連する問題