brennanMKE / DataDataData

Experimentation with Core Data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Double value is stored as a Float in sqlite with Core Data

brennanMKE opened this issue · comments

When creating a Core Data model with a property which is a Double it is stored as a Float in sqlite. To test the edge case the value Double.greatestFiniteMagnitude is stored in the model and it appears as NaN. This was done by starting the app in the iPhone SE Simulator and opening the sqlite database it created. The shell script found in the Gist below is used to find the sqlite database to open it in SQLPro which shows the value for the 3rd object in field2 as NaN and the type for that column as float.

https://gist.github.com/brennanMKE/3bcf4e9dc89287c31f97fc6420d8743a

I also launched the app using iPhone 6s Plus to compare with iPhone SE. It also stored the value as a float and the number appears at NaN as shown below.

data types for sqlite

nan

Printing out the contents of the sqlite database shows there is a value in there. It is unexpected that float is used instead of double. It appears to still work from the Core Data side while SQLPro does not know how to display the number. I would not expect the greatest Double value stored in a float column to display correctly.

let dictionaries: [[String : Any?]] = metas.flatMap { (meta) in
    let dictionary: [String : Any?] = [
        "field1" : meta.field1,
        "field2" : meta.field2,
        "field3" : meta.field3,
        "field4" : meta.field4,
        "field5" : meta.field5
    ]
    return dictionary
}

guard let data = try? JSONSerialization.data(withJSONObject: dictionaries, options: .prettyPrinted),
    let json = String(data: data, encoding: .utf8) else {
        fatalError()
}
debugPrint("json: \(json)")