2016-01-05 6 views
8

私は、最新の「更新された」日付を取得するフェッチ要求を行う必要があるプロジェクトを持っています。しかし、実際に私のクエリによって返された結果を調べると、私は幾分奇妙な振る舞いを見ています。日付を含む「正しい」結果に加えて、結果には空の辞書項目も含まれる。これは、基本となるデータの種類に関係なく、毎回発生します。見知らぬ人でさえ、xcodeでSQLのログインをオンにして、sqllite dbに対してログに記録されたクエリを実行すると、余分なエントリがない正しい結果が得られます。私はここで何が間違っているのかは分かりません。どんな助けもありがたいです。スウィフトフェッチ要求空のアイテムを結果に返す

構築し、クエリを実行機能:取得する

Printing description of results: 
▿ Optional([[:], ["maxUpdated": 2015-12-30 20:05:31 +0000]]) 
    ▿ Some : 2 elements 
    - [0] : 0 elements 
    ▿ [1] : 1 elements 
     ▿ [0] : 2 elements 
     - .0 : "maxUpdated" 
     - .1 : 2015-12-30 20:05:31 +0000 
+0

キャストなしでcontext.executeFetchRequest(request)で印刷できますか?実際に[[String:AnyObject]]ではないものをキャストしている可能性があります。特にSwiftに移植されているobjcのコーナーでは、辞書や配列に続く奇妙なことがあります – tbondwilkinson

+0

あなたは正しい軌道にいると思われますが、印刷物は特に啓発的ではありませんでした。ここに私が得たものがあります:[{ }、{ maxUpdated = "2015-12-30 20:05:31 +0000"; }] – pbuchheit

+0

私はfetchメソッドにブレークポイントを入れて、それをトレースして、どこにリクエストが出てきたか/空のディクショナリがそこに来るのを見ます。 – tbondwilkinson

答えて

1

従来のコアデータの道を:私は最後にブレークポイントを置くと、作成された結果を印刷した場合

func queryForContactDate(context:NSManagedObjectContext) -> AnyObject? 
{ 

    var expressionDescriptions = [AnyObject](); 

    let expressionDescription = NSExpressionDescription() 

    // Name the column 
    expressionDescription.name = "maxUpdated" 
    // Use an expression to specify what aggregate action we want to take and 
    // on which column. In this case max on the update_at column 
    expressionDescription.expression = NSExpression(format: "@max.updated_at") 
    // Specify the return type we expect 
    expressionDescription.expressionResultType = .DateAttributeType 
    // Append the description to our array 
    expressionDescriptions.append(expressionDescription) 

    // Build out our fetch request the usual way 
    let request = NSFetchRequest(entityName: Contact.entityName()) 

    // Specify we want dictionaries to be returned 
    request.resultType = .DictionaryResultType 

    // Hand off our expression descriptions to the propertiesToFetch field. 
    request.propertiesToFetch = expressionDescriptions 

    // Our result is going to be an array of dictionaries. 
    var results:[[String:AnyObject]]? 

    // Perform the fetch. This is using Swfit 2, so we need a do/try/catch 
    do { 
     results = try context.executeFetchRequest(request) as? [[String:AnyObject]] 
    } catch _ { 
     // If it fails, ensure the array is nil 
     results = nil 
    } 

    return results![0]; 
} 

最大値または最小値は、フェッチ制限値1で照会し、そのキーをソートすることです。このObjective-Cのコードのように:

+ (NSFetchRequest *) requestForStatusWithMaxID { 

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName: kAMStatusEntity]; 

    NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey: kAMTwID ascending:NO]; 

    request.sortDescriptors = @[sd]; 
    request.fetchLimit = 1; 

    return request; 

} // +requestForStatusWithMaxID 

あなたのupdated_at財産については、上記を変更するには、非常に簡単になります。

+0

はい、フェッチを行うことは可能ですが、基になるテーブルのサイズが大きくなるにつれてパフォーマンスが低下します。また、SUMのような他の集計関数を試してみると、私はこの同じ振る舞いを見ています。単に並べ替えて、最初の結果を取ることはそれらを助けません。 – pbuchheit

+0

上記のものを含め、インデックスのないクエリは、テーブルが大きくなるにつれてスケーリングの問題があります。あなたのテーブルはどれくらい大きくなっていますか? 100、1,000、10,000、1M行それが大きい場合は、両方のクエリスタイルに対してインデックスが必要です。 – adonoho

関連する問題