AlexDenisov / iActiveRecord

ActiveRecord for iOS without CoreData, only SQLite
http://alexdenisov.github.com/iActiveRecord/
MIT License
354 stars 50 forks source link

Feature/fix date serialization bug #77

Closed neilabdev closed 10 years ago

neilabdev commented 10 years ago

Date serialization doesn't work as expected. The objects returned from sqlite is a NSNumber, which is deserialized using [value doubleValue] ; while the values of unpersisted assignments are NSDates thus causing the getter to raise an selector not found exception as the same function is processing different types. Example test fails without change:

    describe(@"NSDate", ^{
        it(@"Should be saved successfully and return the same date", ^{
            User *alex = [User newRecord];
            alex.name = @"Alex";
            alex.birthDate = [NSDate dateWithTimeIntervalSince1970:0];
            BOOL result = [alex save];

            User *fetchedUser = [[User allRecords] objectAtIndex:0];
            fetchedUser should_not be_nil;
            alex.birthDate should equal(fetchedUser.birthDate);
        });
    });

result would be:

EXCEPTION NSDate NSDate Should be saved successfully into database
-[__NSDate doubleValue]: unrecognized selector sent to instance 0x8ba0eb0
(null)

Solution is to:

NSDate *__strong NSDateColumn::toColumnType(id value) const
{
        if([value isKindOfClass:[NSDate class]])
            return [NSDate dateWithTimeIntervalSince1970: [value timeIntervalSince1970]];
        else if([value isKindOfClass:[NSNumber class]])
            return [NSDate dateWithTimeIntervalSince1970: [value doubleValue]];
        return nil;
}

Instead of just:

NSDate *__strong NSDateColumn::toColumnType(id value) const
{
     return [NSDate dateWithTimeIntervalSince1970: [value doubleValue]];
}
AlexDenisov commented 10 years ago

Thank you!