pbuchheit
pbuchheit

Reputation: 1607

Swift Fetch Request Returning Empty Item In Results

I have a project where I need to do a fetch request that gets the most recent 'updated' date of a Core Data entity. When I actually examine the results returned by my query however I am seeing some rather strange behaviour. In addition to the 'correct' result containing the date the results also include an empty dictionary item. This happens every time, regardless of what the underlying data looks like. Even stranger, if I turn on sql logging in xcode and execute the logged query against the sqllite db it produces the correct result with no extra entries. I'm not quite sure what I'm doing wrong here, any help would be appreciated.

The function that builds and executes the query:

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

If I put a breakpoint at the end and print out the results it produces:

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

Upvotes: 8

Views: 1307

Answers (1)

adonoho
adonoho

Reputation: 4339

The traditional Core Data way to get a max or min item is to query with a fetch limit of 1 and sort on that key. As in this Objective-C code:

+ (NSFetchRequest *) requestForStatusWithMaxID {

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName: kAMStatusEntity];

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

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

    return request;

} // +requestForStatusWithMaxID

It would be quite simple to modify the above for your updated_at property.

Upvotes: 1

Related Questions