I've got an exception sometimes:
failed: No space left on device" UserInfo={NSLocalizedDescription=write() failed: No space left on device, Error Code=1})
I've decided to collect some stats to check if there is really not enough free space on iPhone for Realm to compact its file on launch. When shouldCompactOnLaunch is set. Here is table where one row is one device's available capacity and DB size on that device.
It looks like the available capacity in the most cases is enough. What am I doing wrong?
Why can't the Realm file be initialized?
Here is how Realm is being initialized:
do {
try autoreleasepool {
_ = try Realm(configuration: configuration)
}
} catch let nsError as NSError {
//here is an exceptions occurs
}
This is how I get the available capacity:
private static func getVolumeResourceValues(for key: URLResourceKey) -> URLResourceValues? {
let fileURL = URL(fileURLWithPath: NSHomeDirectory())
let results = try? fileURL.resourceValues(forKeys: [key])
return results
}
private static var systemFreeSize: Int64 {
let systemAttributes = try? Foundation.FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory())
if let space = (systemAttributes?[FileAttributeKey.systemFreeSize] as? NSNumber)?.int64Value {
return space
}
return 0
}
static var availableCapacity: Int64 {
getVolumeResourceValues(for: .volumeAvailableCapacityKey)?.volumeAvailableCapacity.map { Int64($0) } ?? systemFreeSize
}
This is how I get the size of a Realm file:
var totalBytes: UInt64 = 0
if let url = url, let attributes = try? Foundation.FileManager.default.attributesOfItem(atPath: url.path) {
totalBytes = (attributes[.size] as? UInt64) ?? 0
}
